Android基础知识【项目实训】【2】

【该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材】

【项目题目】:校园订餐App设计

综合案例

【目标】

因为项目只涉及基础知识,因此项目中所用数据并不联网,都读取单机数据库。(即将该项目中所用数据,如菜品信息、店铺信息等存入数据库)用户在第一次打开该项目时,会在用户手机上创建这些数据库,并插入测试数据。

1、先制作一个欢迎界面,欢迎的同时,准备数据库

欢迎界面Activity对应的类是FlashActivity,代码如下:

public class FlashActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_flash);
		new Handler().postDelayed(new Runnable(){
			@Override
			public void run() {
				initDb();//	初始化数据库
				Intent intent=new Intent(FlashActivity.this,MainActivity.class);
				startActivity(intent);
				FlashActivity.this.finish();
			}}, 3500);
	}
	public void initDb(){
		EatDbHelper dbh=new EatDbHelper(this,"fooddb.db3",null,1);
		dbh.getReadableDatabase();
	}
}

对应的布局文件是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FlashActivity" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/welcome" />
	<TextView
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:gravity="right"
	    android:text="欢迎使用百米购 V1.0"
	    android:layout_alignParentBottom="true"
	    android:textSize="12sp"
	    />
</RelativeLayout>

2、上面的Activity用到 EatDbHelper类,这是一个操作SQLite数据库的帮助类,下面是代码:

/**
 * 创建数据库的帮助类
 * @author Administrator
 *
 */
public class EatDbHelper extends SQLiteOpenHelper {
	private EatDbHelper dbHelper;

	public EatDbHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}
	public EatDbHelper getInstance(){
		if(dbHelper!=null) return dbHelper;
		dbHelper =this;
		return dbHelper;
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		//创建必须表
		db.execSQL(DbTables.tb_account);	//订单表
		db.execSQL(DbTables.tb_accountItem);	//订单项表
		db.execSQL(DbTables.tb_comment);	//评价表
		db.execSQL(DbTables.tb_foodInfo);	//食品表
		db.execSQL(DbTables.tb_shopInfo);	//店铺表
		db.execSQL(DbTables.tb_userInfo);	//用户信息表
		Log.i("Msg", "创建数据库、 表完成");
		//插入数据
		for (String sql:DbTables.userDb) {
			db.execSQL(sql);
		}
		for (String sql:DbTables.shopDb) {
			db.execSQL(sql);
		}
		for (String sql:DbTables.foodDb) {
			db.execSQL(sql);
		}
		Log.i("Msg", "插入测试数据完成");

	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

	}

}

3、因为需要创建多张表,插入多条测试数据,所用单独为这些Sql语句准备了一个静态类,方便引用(这是非正常做法)

在DbTables这个类中,准备了改项目用到的创建表格Sql语句和插入数据的Sql语句

public class DbTables {
	public static String tb_foodInfo="create table tb_foodInfo(" +
			"_id integer primary key autoincrement," +
			"foodName varchar,price varchar,isPromotion varchar,discount varchar," +
			"category varchar,type varchar,score varchar,shopId integer," +
			"imgId varchar,des varchar" +
			")";

	public static String tb_shopInfo="create table tb_shopInfo(" +
			"_id integer primary key autoincrement," +
			"shopName varchar, phone varchar,address varchar," +
			"score varchar,type varchar,imgId varchar" +
			")";
	public static String tb_userInfo="create table tb_userInfo(" +
			"_id integer primary key autoincrement," +
			"username varchar,loginName varchar,loginpwd varchar," +
			"phone varchar, address varchar,sex varchar,birthday date," +
			"score float, userImage varchar" +
			")";
	public static String tb_comment="create table tb_comment(" +
			"_id integer primary key autoincrement," +
			"userid integer,foodId integer , message varchar,state varchar" +
			")";
	public static String tb_accountItem="create table tb_accountItem(" +
			"_id integer primary key autoincrement," +
			"accountId integer,foodid integer, count integer" +
			")";
	public static String tb_account="create table tb_account(" +
			"_id integer primary key autoincrement," +
			"userid integer,state integer, createDate date" +
			")";
	public static List<String>userDb=new ArrayList<String>();
	public static List<String>shopDb=new ArrayList<String>();
	public static List<String>foodDb=new ArrayList<String>();

	static{
		userDb.add("insert into tb_userInfo(userName,loginName,loginpwd,phone,address," +
				"sex,birthday,score,userImage) " +
				"values('曹操','caocao','caocao','13012345500'," +
				"'师范大学一区25栋10-11','男','1995-10-11','100','0x7f020014')");

		shopDb.add("insert into tb_shopInfo(shopName,phone,address,score,type,imgId)" +
				" values('小张快餐','15566881230','师大西门小吃街','100','1','0x7f020016')");
		shopDb.add("insert into tb_shopInfo(shopName,phone,address,score,type,imgId)" +
				" values('加州面馆','1851357788','师大西门小吃街','500','1','0x7f020016')");
		shopDb.add("insert into tb_shopInfo(shopName,phone,address,score,type,imgId)" +
				" values('大饼夹一切','13051350011','师大西门小吃街','20','1','0x7f020016')");

		foodDb.add("insert into tb_foodInfo(foodName,isPromotion,discount,category," +
				"type,score,shopid,imgId,price)" +
				"values('水果披萨','0','1','西餐','甜香','100','1','0x7f020005','26.00')");
		foodDb.add("insert into tb_foodInfo(foodName,isPromotion,discount,category," +
				"type,score,shopid,imgId,price)" +
				"values('红豆汤圆','0','1','中餐','甜香','100','1','0x7f020006','12.50')");
		foodDb.add("insert into tb_foodInfo(foodName,isPromotion,discount,category," +
				"type,score,shopid,imgId,price)" +
				"values('蔬菜汉堡','1','0.8','西餐','微甜','10','1','0x7f020007','6.00')");
		foodDb.add("insert into tb_foodInfo(foodName,isPromotion,discount,category," +
				"type,score,shopid,imgId,price)" +
				"values('蔬菜沙拉','1','0.6','西餐','甜香','20','1','0x7f020008','5.00')");
		foodDb.add("insert into tb_foodInfo(foodName,isPromotion,discount,category," +
				"type,score,shopid,imgId,price)" +
				"values('芝香烤肉','0','1','中餐','香辣','500','1','0x7f020009','14.00')");
		foodDb.add("insert into tb_foodInfo(foodName,isPromotion,discount,category," +
				"type,score,shopid,imgId,price)" +
				"values('辣味意面','0','1','西餐','香辣','400','1','0x7f02000a','12.00')");
		foodDb.add("insert into tb_foodInfo(foodName,isPromotion,discount,category," +
				"type,score,shopid,imgId,price)" +
				"values('薯条','1','0.9','配菜','香甜','200','1','0x7f02000b','5.00')");
		foodDb.add("insert into tb_foodInfo(foodName,isPromotion,discount,category," +
				"type,score,shopid,imgId,price,des)" +
				"values('烤鸡腿','1','1','中餐','微辣','600','1','0x7f02000c','8.00','精选鸡腿,用心烤制,外焦里内,喷香诱人……')");
	}

}

4、欢迎界面是一个Activity,需要配置:

 <!-- 欢迎界面 -->
        <activity
            android:name="com.example.eatall.FlashActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

5、运行一下看效果:(所有的素材都很随意,请忍耐)

时间: 2024-08-02 07:00:23

Android基础知识【项目实训】【2】的相关文章

Android基础知识【项目实训】【1】

[该项目实训是Android基础知识的一个综合练习] [项目题目]:校园订餐App设计 综合案例 [设计目标] 1.必要功能 ?快餐店浏览,与订餐 ?今天订餐活动查询与订餐,特价饭菜预定 ?分类订餐查询,预定 ?常定饭菜  预定 ?健康餐推荐 ?定时预定,提前预定 ?订单查看, ?餐馆与饭菜打分,评价 ?用户注册与登录 2.扩展选择功能 ?快速拨打电话 ?饮食跟踪,热量估算 ?系统设置 [项目说明] 该项目为实际应用项目的单机 简化版本,只需要完成Android平台App的设计与开发工作. Ap

Android基础知识【项目实训】【3】

[该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材] [项目题目]:校园订餐App设计 综合案例 [目标] 欢迎界面过后,应该显示app的主界面了,根据[UI设计指导]中的规划,主界面采用上下两级标签导航.这部分是app开发中比较麻烦的一块. 1.先来看一下,最终的效果吧,这样做起来比较有底: 默认显示的主界面,下部是主导航,上面是二级导航,默认打开的是"促销打折"这一版面

Android基础知识【项目实训】【4】

[该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材] [项目题目]:校园订餐App设计 综合案例 [目标] 主界面的功能确实比较复杂,因此上一篇知识说的周边内容.现在开始说这个界面的代码和布局文件. 1.先看一下项目的组织结构吧,要不然不好说他们的关系: (1)db包中的都是跟 数据库相关的 (2)eatall中放的都是activity或者fragment (3)entity中放的实

Android基础知识【项目实训】【5】

[该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材] [项目题目]:校园订餐App设计 综合案例 [目标] 主界面中包含两个二级子界面,分别是活动界面和账单界面,下面介绍它们的实现代码和布局文件. 1.下面这个是 活动界面的Activity代码,因为这个界面加载时需要 读取数据库中数据了,所有功能的实现上会涉及到 db那个包中一些类. 注意这个Activity也是继承 Activit

BeagleBone Black项目实训手册(大学霸内部资料)

BeagleBone Black项目实训手册(大学霸内部资料) 介绍:本教程是<BeagleBone Black快速入门教程>的后续教程.本教程以项目操作为主,讲解LED项目.声音项目.传感器项目以及显示项目,并对Beaglebone Black的GPIO.PWM以及I2C等特殊的接口进行详细讲解. 试读下载地址:http://pan.baidu.com/s/1eQozxnG BeagleBone Black项目实训手册 目  录 第1章  准备开始 1 1.1  启动你的Beaglebone

【CC2530入门教程-增强版】基础技能综合实训案例(基础版)-终端源码

[CC2530入门教程-增强版]基础技能综合实训案例(基础版)-终端源码 广东职业技术学院 欧浩源 一.关于硬件电路 关于这个综合实训案例,具体需求详见<[CC2530入门教程-增强版]基础技能综合实训案例(基础版)-题目需求>. 我自己实在"全国职业院校技能大赛--物联网技术应用赛项"的Zigbee模块上实现的.该模块的电路应该和TI公司官方评估板的推荐电路差不多,我想现在市面上很多开发板也是参考这样的电路设计,只要您使用的开发板上有LED灯.按键输入.串口输出和一路A/

Android基础整合项目之节日群发助手(一)

Android基础整合项目(一) 之节日群发助手part 1 --转载请注明出处:coder-pig 本节引言: Android入门系列已经写了大半了,学习了这么多理论知识,不练下手怎么行呢? 在实际的开发中我们会遇到更多的问题,同时也能加固我们的基础知识!鉴于 笔者的水平有限,该项目,面对的是初学者,各位大牛路过不喜勿喷!好吧说下第一个 练手项目吧,前几天中秋节今天又是教师节,各种祝福短信满天飞,手打再群发,条条 短信一个样,没意思!直接用别人弄好的短信群发,别人又不知道你是谁,起码加个: 亲

android基础知识

1. 前言 1.1. 什么是3G.4G Ÿ 第三代移动通信技术(3rd - Generation),速率一般在几百Kbps,较之前的2G和2.5G在数据传输速度上有很大提升. Ÿ 第四代移动通信技术(4th - Generation),速度可达到100Mbps以上,几乎可以满足人们的所有传输数据的需求. Ÿ 目前主流的3G技术标准有三种: WCDMA:全球80%以上的3G网络都是采用此种制式.中国联通运营.186 CDMA2000:目前日韩及北美使用较多.中国电信运营. 189 TD-SCDMA

Android基础知识(6)—数据持久化之数据存储

阅读前,请浏览此处上方目录. Android基础知识(6)-数据持久化之数据存储 本章内容为个人笔记,参考书籍有:<疯狂的android>第3版.<第一行代码> 首先,我们要知道什么是数据持久化. 数据持久化就是指那些内存中的瞬时数据保存到存储设备中,保证即使手机在关机的情况下,这些数据不会丢失.保存在内存中的数据是处于瞬时状态,保存在存储设备中的数据是处于持久状态.持久化技术则是提供了一种机制可以让数据在瞬时状态和持久状态之间进行转换. Android系统主要提供了三种方式用于简