首页 > 代码库 > Android基础知识【项目实训】【5】

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

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

【项目题目】:校园订餐App设计
综合案例
【目标】

主界面中包含两个二级子界面,分别是活动界面和账单界面,下面介绍它们的实现代码和布局文件。

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 =http://www.mamicode.com/new FoodDao().queryFood(db, >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导航,只是它的标签导航在上面。
3、因为要读取活动和折扣 食物数据,因此准备了两个集合
//促销列表数据,
	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 + "]";
	}
	
}

4、上面的俩集合中的数据是靠 db中的类给填充的。

pdata =http://www.mamicode.com/new FoodDao().queryFood(db, >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 = http://www.mamicode.com/data;>
6、其中 列表
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=http://www.mamicode.com/"@drawable/food_02" />>

折扣界面说完,下一篇说 账单界面。

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