Android Basics "Project Training" "5"

Source: Internet
Author: User

"The project training is a comprehensive exercise of Android fundamentals, in particular: the project will use some of the picture material, are randomly organized, will later upload a resource, including the item's basic function, also contains the picture material "

"Project title": Campus booking app Design comprehensive case "target"

The main interface contains two levels of two sub-interfaces, namely the active interface and the billing interface, which describe their implementation code and layout files.

1, the following is the activity code of the active interface, because this interface needs to read the data in the database, the implementation of all the functions will involve db that package of some classes.

Note that this activity is also inherited from Activitygroup.

public class Discountfoodactivity extends Activitygroup implements Onitemclicklistener {Tabhost innertab; ListView promotionfoodlist,discountfoodlist;//Promotional list data,list<foodinfo> pdata;//discount list data list<foodinfo> Ddata; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_discount_food);p romotionfoodlist= (ListView) Findviewbyid ( r.id.promotionfoodlist);d iscountfoodlist= (ListView) Findviewbyid (r.id.discountfoodlist); innertab= (TabHost) Findviewbyid (R.id.innertab_discount);//Initialize the internal tabinitinnertabhost (); Initlistview ();//Initialize list data//Add listener, Click Promotionfoodlist.setonitemclicklistener (this) for the Listener list item;d Iscountfoodlist.setonitemclicklistener (this);} Initialize activity food list and discount food list private void Initlistview () {//Get database Eatdbhelper dbh=new eatdbhelper (This, "fooddb.db3", null,1); Sqlitedatabase db =dbh.getreadabledatabase (); pdata =new Fooddao (). Queryfood (db, "ispromotion=?", New string[]{"1"}, "Price DESC"); Foodlistadapter FLA =new Foodlistadapter (This,pdata,r.layout.foodlist_item); PROMOTIONFOODLIST.SETADAPTER (FLA);dd ATA =new Fooddao (). Queryfood (db, "DISCOUNT&LT;?", New string[]{"1"}, "Price DESC" ); Fla =new Foodlistadapter (This,ddata,r.layout.foodlist_item); DISCOUNTFOODLIST.SETADAPTER (FLA);} private void Initinnertabhost () {Innertab.setup (Getlocalactivitymanager ()); Tabspec T1=innertab.newtabspec ("Inner_dis_t1"); T1.setindicator ("Activities Today", Getresources () getdrawable (R.drawable.ic_ launcher)); T1.setcontent (R.ID.INNERTAB_DISCOUNT_TAB1); Tabspec T2=innertab.newtabspec ("Inner_dis_t2"); T2.setindicator ("Today's Discount", Getresources (). Getdrawable (R.drawable.ic_ launcher)); T2.setcontent (R.ID.INNERTAB_DISCOUNT_TAB2); Innertab.addtab (t1); Innertab.addtab (T2);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.discount_food, menu); return true;} @Overridepublic void Onitemclick (adapterview<?> lv, view view, int index, long arg3) {Intent Intent =New Intent (This,fooddetailactivity.class); BUNDLE BD =new Bundle ();//LOG.I ("MSG", "Current click List" +lv + "" +lv.getid ()); if (Lv.getid () ==r.id.promotionfoodlist) { Bd.putserializable ("Food", Pdata.get (Index)),//LOG.I ("MSG", "Current selection:" +pdata.get (Index). Getfoodname ());} else if (Lv.getid () ==r.id.discountfoodlist) {bd.putserializable ("food", Ddata.get (index));//LOG.I ("MSG", "Current selection:" + Ddata.get (Index). Getfoodname ());} Intent.putextras (BD); startactivity (intent);}}
2, layout interface, the project uses a lot of resources, now the results of these resources to show.


The interface used for activity activities is activity_discount_food.xml, and the code is:

<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 "Tools:context =". Discountfoodactivity "> <tabhost android:id=" @+id/innertab_discount "android:layout_width=" Match_pa Rent "android:layout_height=" match_parent "android:layout_alignparentleft=" true "android:layout_align Parenttop= "true" > <linearlayout android:layout_width= "match_parent" Android:layout_hei ght= "match_parent" android:orientation= "vertical" > <tabwidget android:id= "@and            Roid:id/tabs "android:layout_width=" match_parent "android:layout_height=" Wrap_content ">                </TabWidget> <framelayout android:id= "@android: Id/tabcontent"      Android:layout_width= "Match_parent"          android:layout_height= "Match_parent" > <linearlayout android:id= "@+id/in Nertab_discount_tab1 "android:layout_width=" match_parent "android:layout_height=" mat                         Ch_parent "android:orientation=" vertical "> <listview                         Android:id= "@+id/promotionfoodlist" android:layout_width= "Match_parent"                 android:layout_height= "Match_parent" > </ListView> </LinearLayout> <linearlayout android:id= "@+id/innertab_discoun                    T_tab2 "android:layout_width=" match_parent "android:orientation=" vertical " android:layout_height= "Match_parent" > <listview android:id= "@+    Id/discountfoodlist "                     Android:layout_width= "Match_parent" android:layout_height= "Match_parent" > </ListView> </LinearLayout> </framel Ayout> </LinearLayout> </TabHost></RelativeLayout>
Actually this is also a tab navigation, just its label navigation on top.
3. Two collections are prepared because you want to read activity and discounted food data
Promotional list data,list<foodinfo> pdata;//discount list data list<foodinfo> Ddata;

The Foodinf here is an entity class, please note the comment on the attribute.

Package Com.example.entity;import Java.io.serializable;import Android.graphics.bitmap;public class FoodInfo Implements Serializable {private string _id,foodname;private string Price;private string ispromotion;//Menu Promotions 1 active, 0 not There are activities private string discount= "1";//discount private string category;//food belongs to category, staple, Dish, drink, other four kinds of private string type;//cuisines, sweet and sour salty, Lu Yue Chunsu and other private String score;//food cumulative score private int shopid;//restaurant idprivate shopinfo shop;//Food-owned restaurant private Bitmap Img;private String Imgid;private string description;//Description of the food public String Getfoodname () {return foodname;} public void Setfoodname (String foodname) {this.foodname = Foodname;} Public String GetPrice () {return price;} public void Setprice (String price) {this.price = Price;} Public String GetCategory () {return category;} public void Setcategory (String category) {this.category = category;} Public String GetType () {return type;} public void SetType (String type) {this.type = type;} Public String Getscore () {return score;} public void SetScore (String score) {thIs.score = score;} Public Shopinfo Getshop () {return shop;} public void Setshop (Shopinfo shop) {this.shop = shop;} Public String get_id () {return _id;} public void set_id (String _id) {this._id = _id;} public int getshopid () {return shopid;} public void setshopid (int shopid) {this.shopid = Shopid;} Public String getispromotion () {return ispromotion;} public void Setispromotion (String ispromotion) {this.ispromotion = ispromotion;} Public String Getdiscount () {return discount;} public void SetDiscount (String discount) {this.discount = discount;} Public Bitmap getimg () {return img;} public void Setimg (Bitmap img) {this.img = img;} Public String Getimgid () {return imgid;} public void Setimgid (String imgid) {this.imgid = Imgid;} Public String GetDescription () {return description;} public void SetDescription (String description) {this.description = description;} @Overridepublic String toString () {return "Foodinfo [_id=" + _id + ", foodname=" + Foodname + ", price=" + Price + ", Ispro Motion= "+ Ispromotion +",Discount= "+ Discount +", category= "+ category +", type= "+ type+", score= "+ score +", shopid= "+ Shopid +", shop= " + shop+ ", img=" + img + ", imgid=" + Imgid + "]";}}

4. The data in the two sets above is filled by the class in the DB.

pdata =new Fooddao (). Queryfood (db, "ispromotion=?", New string[]{"1"}, "Price DESC");
Fooddao is a class that specializes in reading food data, with the following code:

public class Fooddao {public list<foodinfo> queryfood (sqlitedatabase db,string sel,string []selarg,string order) {List< Foodinfo> fs=new arraylist<foodinfo> (); Cursor c=db.query ("Tb_foodinfo", New string[]{"_id", "Foodname", "category", "type", "Price", "Score", "Imgid", "Shopid" , "Discount", "Ispromotion"}, Sel,selarg,null,null,order); LOG.I ("MSG", "read record number:" +c.getcount ()); C.movetofirst (); while (!c.isafterlast ()) {Foodinfo F =new foodinfo (); F.SET_ID ( C.getstring (0)); F.setfoodname (c.getstring (1)); F.setcategory (C.getstring (2)); F.settype (C.getstring (3)); F.setprice (C.getstring (4)); F.setscore (C.getstring (5)); String imgid=c.getstring (6); F.setimgid (Integer.parseint (imgid.substring (2), +) + ""); F.setshopid (C.getint (7)); F.setdiscount (c.getstring (8)), F.setispromotion (c.getstring (9)),//LOG.I ("MSG", F.tostring ()), Fs.add (f); C.movetonext ();} C.close (); return FS;}} 
5, after reading the data, you can use the adapter to bind the data to the list.

Foodlistadapter FLA =new Foodlistadapter (
This,pdata,r.layout.foodlist_item);

Foodlistadapter is an adapter that inherits the Baseadapter. (This adapter is not universally written during easy comprehension, but it should be easy to understand)

The code is as follows:

public class Foodlistadapter extends Baseadapter {private Context context;private list<foodinfo> data;private int L Ayout;public Foodlistadapter (context context, list<foodinfo> data, int layout) {super (); this.context = Context; This.data = Data;this.layout = layout;} @Overridepublic int GetCount () {return data.size ();} @Overridepublic Object getItem (int arg0) {//TODO auto-generated method Stubreturn Data.get (arg0);} @Overridepublic long getitemid (int arg0) {//TODO auto-generated method Stubreturn arg0;} @Overridepublic view GetView (int index, view arg1, ViewGroup arg2) {layoutinflater Inflater =layoutinflater.from (context ); View v =inflater.inflate (layout, null), ImageView IV = (ImageView) V.findviewbyid (r.id.foodlist_item_img); Iv.setimageresource (Integer.parseint (Data.get (index). Getimgid ())); TextView name= (TextView) V.findviewbyid (r.id.foodlist_item_name); Name.settext (Data.get (index). Getfoodname ()); TextView category= (TextView) V.findviewbyid (r.id.foodlist_item_category); category.SetText (Data.get (index). GetCategory ()); TextView type= (TextView) V.findviewbyid (R.id.foodlist_item_type); Type.settext (Data.get (index). GetType ()); TextView price= (TextView) V.findviewbyid (r.id.foodlist_item_price);p rice.settext ("¥" +data.get (Index). GetPrice ()) ; float ZK =float.parsefloat (data.get (Index) Getdiscount ()); if (zk<1) {TextView discount= (TextView) V.findviewbyid (R.id.foodlist_item_discount);d iscount.settext ("Discount:" + (ZK*10) + "fold");} TextView score= (TextView) V.findviewbyid (r.id.foodlist_item_score) score.settext ("Hundred Meters:" +data.get (index). Getscore () + "M"); return v;}}

6, which list
ListView promotionfoodlist,discountfoodlist;
Their list item layouts are the same, with a unified layout file:
<pre name= "code" class= "HTML" ><?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= " Http://schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:layout_height=" Match_ Parent "> <imageview android:id=" @+id/foodlist_item_img "android:layout_width=" 60DP "Android : layout_height= "60DP" android:background= "@drawable/imgbg" android:src= "@drawable/food_02"/> <text View android:id= "@+id/foodlist_item_name" android:layout_width= "Wrap_content" android:layout_height= " Wrap_content "android:layout_torightof=" @id/foodlist_item_img "android:layout_marginleft=" 8DP "Androi d:text= "Dishes" android:textappearance= "Android:attr/textappearancelarge"/> <textview android:id= "@+i D/foodlist_item_category "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "an droid:layout_torightof= "@id/foodlist_itEm_name "android:layout_marginleft=" 8DP "android:text=" Chinese "android:textappearance="? Android:attr/text Appearancemedium "/> <textview android:id=" @+id/foodlist_item_type "android:layout_width=" Wrap_cont Ent "android:layout_height=" wrap_content "android:layout_torightof=" @id/foodlist_item_category "Andro id:layout_marginleft= "8DP" android:text= "hot and Sour" android:textappearance= "Android:attr/textappearancemedium"/&G    T <textview android:id= "@+id/foodlist_item_price" android:layout_width= "Wrap_content" android:layout _height= "Wrap_content" android:layout_below= "@id/foodlist_item_name" android:layout_alignleft= "@id/foodlist_item _name "android:layout_margintop=" 8DP "android:text=" ¥10.00 "android:textappearance="? Android:attr/tex Tappearancesmall "/> <textview android:id=" @+id/foodlist_item_discount "android:layout_width=" Wrap_ Content "Android:layout_height= "Wrap_content" android:layout_torightof= "@id/foodlist_item_price" android:layout_alignbottom= "@id/fo Odlist_item_price "android:layout_marginleft=" 8DP "android:text=" "android:textappearance="? android:a Ttr/textappearancesmall "/> <textview android:id=" @+id/foodlist_item_score "Android:layout_width=" W        Rap_content "android:layout_height=" wrap_content "android:layout_torightof=" @id/foodlist_item_discount " android:layout_alignbottom= "@id/foodlist_item_discount" android:layout_alignparentright= "true" Android:layout_ marginright= "8DP" android:gravity= "right" android:text= "rating: 122" android:textappearance= "? android:attr /textappearancesmall "/></relativelayout>


The discount interface is finished and the next page says the billing interface.

Android Basics "Project Training" "5"

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.