【该项目实训是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