Android Basics "Project Training" "2"

Source: Internet
Author: User

"The project training is a comprehensive exercise of Android fundamentals, with special hints: some of the image materials will be used in the project,A resource will be uploaded later containing the basic functionality of the item, as well as the image material】
"Project title": Campus booking app Design comprehensive case "target"Because the project only covers the basics, the data used in the project is not networked and reads the stand-alone database. (data to be used in the project, such as food information, store information, etc.) when the user opens the project for the first time, the database is created on the user's phone and the test data is inserted. 1, first create a welcome interface, welcome to the same time, prepare the databaseWelcome interface activity corresponds to the class is flashactivity, the code is as follows:
public class Flashactivity extends Activity {@Overrideprotected 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 () {@Overridepublic void run () {initdb ();// Initialize Database 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);d bh.getreadabledatabase ();}}
the corresponding layout files are:
<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:paddi ngbottom= "@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_h    eight= "Wrap_content" android:gravity= "right" android:text= "Welcome to Hundred meters V1.0" android:layout_alignparentbottom= "true" Android:textsize= "12SP"/></relativelayoUt> 
2, the above activity to useEatdbhelper class, which is a helper class for manipulating the SQLite database, here is the code:
/** * Help class to create a database * @author Administrator * */public class Eatdbhelper extends Sqliteopenhelper {private Eatdbhelper Dbhelpe R;public Eatdbhelper (Context context, String name, cursorfactory factory,int version) {Super (context, name, factory, vers ION);} Public Eatdbhelper getinstance () {if (dbhelper!=null) return dbhelper;dbhelper =this;return DBHelper;} @Overridepublic void OnCreate (Sqlitedatabase db) {//Create must table Db.execsql (dbtables.tb_account);//Order Table Db.execsql ( Dbtables.tb_accountitem)///Order Item table Db.execsql (dbtables.tb_comment);//Evaluation Table Db.execsql (DBTABLES.TB_FOODINFO);// Food table Db.execsql (dbtables.tb_shopinfo);//Shop Table Db.execsql (dbtables.tb_userinfo);//User Information table log.i ("MSG", "CREATE database, Table complete");// Insert data 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", "Insert test data Complete");} @Overridepublic void Onupgrade (sqlitedatabase arg0, int arg1, int arg2) {}}
3, because you need to create more than one table, insert multiple test data, the use of separate for these SQL statements prepared a static class, easy to reference (this is not normal practice) indbtables in this class, prepare the SQL statement to create the table SQL statement and insert the data for the project
public class Dbtables {public static String tb_foodinfo= "CREATE TABLE Tb_foodinfo (" + "_id Integer primary key autoincremen T, "+" Foodname varchar,price varchar,ispromotion varchar,discount varchar, "+" category Varchar,type Varchar,score Varchar,shopid integer, "+" imgid varchar,des varchar "+") ";p ublic 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 "+") ";p ublic 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," + "s Core float, userimage varchar "+") ";p ublic static String tb_comment=" CREATE TABLE tb_comment ("+" _id integer primary Key A Utoincrement, "+" userid integer,foodid integer, message varchar,state varchar "+") ";p ublic static String tb_accountitem= "CREATE TABLE Tb_accountitem (" + "_id Integer PRIMARY key autoincrement, "+" AccountId integer,foodid integer, Count integer "+") ";p ublic static String Tb_acco unt= "CREATE TABLE Tb_account (" + "_id integer primary Key autoincrement," + "userid integer,state integer, createdate date" + ")";p ublic static List<string>userdb=new arraylist<string> ();p ublic static list<string>shopdb= New Arraylist<string> ();p ublic 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 ', ' Caocao ', ' 13012345500 ', ' + ') ' Normal University area 25 10-11 ', ' Male ', ' 1995-10-11 ', ' + ', ' 0x7f020014 ') '; Shopdb.add ("INSERT into Tb_shopinfo (shopname,phone,address,score,type,imgid)" + "Values" (' Xiao Zhang fast food ', ' 15566881230 ', ' Normal University ximen Snack Street ', ' + ', ' 1 ', ' 0x7f020016 '); Shopdb.add ("INSERT into Tb_shopinfo (Shopname,phone,address,score,type,imgid)" + "Values" (' California noodle Shop ', ' 1851357788 ', ' Normal University ximen Snack Street ', ' $ ', ' 1 ', ' 0x7f020016 ') '); Shopdb.add ("INSERT INTO tb_sHopinfo (Shopname,phone,address,score,type,imgid) "+" VALUES (' Cake clip everything ', ' 13051350011 ', ' Normal University ximen Snack Street ', ' 20 ', ' 1 ', ' 0x7f020016 '); Fooddb.add ("INSERT into Tb_foodinfo (foodname,ispromotion,discount,category," + "type,score,shopid, Imgid,price) "+" values (' Fruit pizza ', ' 0 ', ' 1 ', ' Western ', ' sweet ', ' + ', ' 1 ', ' 0x7f020005 ', ' 26.00 ') '); Fooddb.add ("INSERT INTO tb_ Foodinfo (foodname,ispromotion,discount,category, "+" Type,score,shopid,imgid,price) "+" values (' Red bean Dumpling ', ' 0 ', ' 1 ', ' Chinese ') , ' sweet ', ' + ', ' 1 ', ' 0x7f020006 ', ' 12.50 '), Fooddb.add ("INSERT into Tb_foodinfo (Foodname,ispromotion,discount, Category, "+" Type,score,shopid,imgid,price) "+" values (' Vegetable burger ', ' 1 ', ' 0.8 ', ' Western ', ' Sweet ', ' ten ', ' 1 ', ' 0x7f020007 ', ' 6.00 ') ') ; Fooddb.add ("INSERT into Tb_foodinfo (foodname,ispromotion,discount,category," + "Type,score,shopid,imgid,price)" + " VALUES (' Vegetable salad ', ' 1 ', ' 0.6 ', ' Western-style ', ' Sweet ', ' ' ' ', ' 1 ', ' 0x7f020008 ', ' 5.00 ') '); Fooddb.add ("INSERT into Tb_foodinfo (Foodname, Ispromotion,discount,category, "+" Type,score,shopid,imgid,price) "+" VALUES (' Shiba-grilled ', ' 0 ', ' 1 ', ' Chinese ', ' spicy ', ' 500 ', ' 1 ', ' 0x7f020009 ', ' 14.00 '); Fooddb.add ("INSERT into Tb_foodinfo (foodname,ispromotion,discount,category," + "type,score,shopid, Imgid,price) "+" values (' spicy pasta ', ' 0 ', ' 1 ', ' Western ', ' spicy ', ' + ', ' 1 ', ' 0x7f02000a ', ' 12.00 ') '); Fooddb.add ("INSERT INTO tb_ Foodinfo (foodname,ispromotion,discount,category, "+" Type,score,shopid,imgid,price) "+" VALUES (' fries ', ' 1 ', ' 0.9 ', ' matching dishes ') , ' Sweet ', ' n ', ' 1 ', ' 0x7f02000b ', ' 5.00 '), Fooddb.add ("INSERT into Tb_foodinfo (Foodname,ispromotion,discount, Category, "+" type,score,shopid,imgid,price,des) "+" values (' Grilled chicken legs ', ' 1 ', ' 1 ', ' Chinese food ', ' spicy ', ' + ', ' 1 ', ' 0x7f02000c ', ' 8.00 ', ' Selection of chicken legs, baked in the heart, inside the outside coke, fragrant and inviting ... ');}
4, welcome interface is an activity, need to configure:
<!--Welcome interface--        <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, run a look at the effect: (all materials are very casual, please endure)

Android Basics "Project Training" "2"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.