标签:android
【该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材】
主界面中包含两个二级子界面,分别是活动界面和账单界面,下面介绍它们的实现代码和布局文件。
1、下面这个是 活动界面的Activity代码,因为这个界面加载时需要 读取数据库中数据了,所有功能的实现上会涉及到 db那个包中一些类。
注意这个Activity也是继承 ActivityGroup的
public class DiscountFoodActivity extends ActivityGroup implements OnItemClickListener { TabHost innerTab; ListView promotionFoodList,discountFoodList; //促销列表数据, List<FoodInfo> pdata; //折扣列表数据 List<FoodInfo> ddata; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_discount_food); promotionFoodList=(ListView) findViewById(R.id.promotionFoodList); discountFoodList=(ListView) findViewById(R.id.discountFoodList); innerTab=(TabHost) findViewById(R.id.innerTab_discount); //初始化的内部Tab initInnerTabHost(); initListView(); //初始化列表数据 //添加监听器,监听列表项的点击 promotionFoodList.setOnItemClickListener(this); discountFoodList.setOnItemClickListener(this); } //初始化活动食品列表 和 折扣食品列表 private void initListView() { //获取数据库 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); ddata =new FoodDao().queryFood(db, "discount<?", 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("今日活动", getResources().getDrawable(R.drawable.ic_launcher)); t1.setContent(R.id.innerTab_discount_tab1); TabSpec t2=innerTab.newTabSpec("inner_dis_t2"); t2.setIndicator("今日折扣", getResources().getDrawable(R.drawable.ic_launcher)); t2.setContent(R.id.innerTab_discount_tab2); innerTab.addTab(t1); innerTab.addTab(t2); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.discount_food, menu); return true; } @Override public void onItemClick(AdapterView<?> lv, View view, int index, long arg3) { Intent intent =new Intent(this,FoodDetailActivity.class); Bundle bd =new Bundle(); //Log.i("Msg", "当前点击列表"+lv + " "+lv.getId()); if(lv.getId()==R.id.promotionFoodList){ bd.putSerializable("food",pdata.get(index)); //Log.i("Msg", "当前选择:"+pdata.get(index).getFoodName()); }else if(lv.getId()==R.id.discountFoodList){ bd.putSerializable("food",ddata.get(index)); //Log.i("Msg", "当前选择:"+ddata.get(index).getFoodName()); } intent.putExtras(bd); startActivity(intent); } }2、布局界面,项目中用到很多资源,现在把这些资源的结果 展示一下。
活动Activity使用的界面是activity_discount_food.xml,代码是:
<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_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget android:id="@android: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/innerTab_discount_tab1" android:layout_width="match_parent" android:layout_height="match_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_discount_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> </FrameLayout> </LinearLayout> </TabHost> </RelativeLayout>其实这也是一个tab导航,只是它的标签导航在上面。
//促销列表数据, List<FoodInfo> pdata; //折扣列表数据 List<FoodInfo> ddata;
这里的FoodInf是一个实体类,请留意对属性的注释。
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; //菜品是否促销活动 1 有活动, 0 没有活动 private String discount="1"; //折扣 private String category; //食物所属类别,主食、配菜、饮料,其他四类 private String type; //菜系,酸甜苦辣咸等,鲁粤川苏等 private String score; //菜品累计得分 private int shopId; //餐馆ID private ShopInfo shop; //食物所属餐馆 private Bitmap img; private String imgId; private String description; //关于食物的描述 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; } @Override public String toString() { return "FoodInfo [_id=" + _id + ", foodName=" + foodName + ", price=" + price + ", isPromotion=" + isPromotion + ", discount=" + discount + ", category=" + category + ", type=" + type + ", score=" + score + ", shopId=" + shopId + ", shop=" + shop + ", img=" + img + ", imgId=" + imgId + "]"; } }
pdata =new FoodDao().queryFood(db, "ispromotion=?", new String[]{"1"}, "price DESC");FoodDao就是一个专门读取 食物数据的类,其代码如下:
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", "读取记录条数 :"+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),16)+""); 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、读取完数据了,就可以采用适配器 把数据绑定到 列表上了。
FoodListAdapter fla =new FoodListAdapter(
this,pdata,R.layout.foodlist_item);
FoodListAdapter是一个继承了BaseAdapter的适配器。( 容易理解期间,这个适配器写的并不通用,但应该容易理解了)
代码如下:
public class FoodListAdapter extends BaseAdapter { private Context context; private List<FoodInfo> data; private int layout; public FoodListAdapter(Context context, List<FoodInfo> data, int layout) { super(); this.context = context; this.data = data; this.layout = layout; } @Override public int getCount() { return data.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return data.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public 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); price.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); discount.setText("折扣:"+(zk*10)+"折"); } TextView score=(TextView) v.findViewById(R.id.foodlist_item_score); score.setText("百米分:"+data.get(index).getScore()+"米"); return v; } }
ListView promotionFoodList,discountFoodList;
它们的列表项布局是一样的,统一采用布局文件:
<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" /> <TextView 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" android:text="菜品" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/foodlist_item_category" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/foodlist_item_name" android:layout_marginLeft="8dp" android:text="中餐" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/foodlist_item_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/foodlist_item_category" android:layout_marginLeft="8dp" android:text="酸辣" android:textAppearance="?android:attr/textAppearanceMedium" /> <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/textAppearanceSmall" /> <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/foodlist_item_price" android:layout_marginLeft="8dp" android:text="" android:textAppearance="?android:attr/textAppearanceSmall" /> <TextView android:id="@+id/foodlist_item_score" android:layout_width="wrap_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="评分:122" android:textAppearance="?android:attr/textAppearanceSmall" /> </RelativeLayout>
折扣界面说完,下一篇说 账单界面。
标签:android
原文地址:http://blog.csdn.net/freeman527/article/details/42168021