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 =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导航,只是它的标签导航在上面。

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 =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;
	}

}

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="@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>

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

时间: 2024-08-02 11:03:54

Android基础知识【项目实训】【5】的相关文章

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

[该项目实训是Android基础知识的一个综合练习] [项目题目]:校园订餐App设计 综合案例 [设计目标] 1.必要功能 ?快餐店浏览,与订餐 ?今天订餐活动查询与订餐,特价饭菜预定 ?分类订餐查询,预定 ?常定饭菜  预定 ?健康餐推荐 ?定时预定,提前预定 ?订单查看, ?餐馆与饭菜打分,评价 ?用户注册与登录 2.扩展选择功能 ?快速拨打电话 ?饮食跟踪,热量估算 ?系统设置 [项目说明] 该项目为实际应用项目的单机 简化版本,只需要完成Android平台App的设计与开发工作. Ap

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

[该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材] [项目题目]:校园订餐App设计 综合案例 [目标] 因为项目只涉及基础知识,因此项目中所用数据并不联网,都读取单机数据库.(即将该项目中所用数据,如菜品信息.店铺信息等存入数据库)用户在第一次打开该项目时,会在用户手机上创建这些数据库,并插入测试数据. 1.先制作一个欢迎界面,欢迎的同时,准备数据库 欢迎界面Activity对应

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

[该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材] [项目题目]:校园订餐App设计 综合案例 [目标] 欢迎界面过后,应该显示app的主界面了,根据[UI设计指导]中的规划,主界面采用上下两级标签导航.这部分是app开发中比较麻烦的一块. 1.先来看一下,最终的效果吧,这样做起来比较有底: 默认显示的主界面,下部是主导航,上面是二级导航,默认打开的是"促销打折"这一版面

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

[该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材] [项目题目]:校园订餐App设计 综合案例 [目标] 主界面的功能确实比较复杂,因此上一篇知识说的周边内容.现在开始说这个界面的代码和布局文件. 1.先看一下项目的组织结构吧,要不然不好说他们的关系: (1)db包中的都是跟 数据库相关的 (2)eatall中放的都是activity或者fragment (3)entity中放的实

BeagleBone Black项目实训手册(大学霸内部资料)

BeagleBone Black项目实训手册(大学霸内部资料) 介绍:本教程是<BeagleBone Black快速入门教程>的后续教程.本教程以项目操作为主,讲解LED项目.声音项目.传感器项目以及显示项目,并对Beaglebone Black的GPIO.PWM以及I2C等特殊的接口进行详细讲解. 试读下载地址:http://pan.baidu.com/s/1eQozxnG BeagleBone Black项目实训手册 目  录 第1章  准备开始 1 1.1  启动你的Beaglebone

【CC2530入门教程-增强版】基础技能综合实训案例(基础版)-终端源码

[CC2530入门教程-增强版]基础技能综合实训案例(基础版)-终端源码 广东职业技术学院 欧浩源 一.关于硬件电路 关于这个综合实训案例,具体需求详见<[CC2530入门教程-增强版]基础技能综合实训案例(基础版)-题目需求>. 我自己实在"全国职业院校技能大赛--物联网技术应用赛项"的Zigbee模块上实现的.该模块的电路应该和TI公司官方评估板的推荐电路差不多,我想现在市面上很多开发板也是参考这样的电路设计,只要您使用的开发板上有LED灯.按键输入.串口输出和一路A/

Android基础整合项目之节日群发助手(一)

Android基础整合项目(一) 之节日群发助手part 1 --转载请注明出处:coder-pig 本节引言: Android入门系列已经写了大半了,学习了这么多理论知识,不练下手怎么行呢? 在实际的开发中我们会遇到更多的问题,同时也能加固我们的基础知识!鉴于 笔者的水平有限,该项目,面对的是初学者,各位大牛路过不喜勿喷!好吧说下第一个 练手项目吧,前几天中秋节今天又是教师节,各种祝福短信满天飞,手打再群发,条条 短信一个样,没意思!直接用别人弄好的短信群发,别人又不知道你是谁,起码加个: 亲

android基础知识

1. 前言 1.1. 什么是3G.4G Ÿ 第三代移动通信技术(3rd - Generation),速率一般在几百Kbps,较之前的2G和2.5G在数据传输速度上有很大提升. Ÿ 第四代移动通信技术(4th - Generation),速度可达到100Mbps以上,几乎可以满足人们的所有传输数据的需求. Ÿ 目前主流的3G技术标准有三种: WCDMA:全球80%以上的3G网络都是采用此种制式.中国联通运营.186 CDMA2000:目前日韩及北美使用较多.中国电信运营. 189 TD-SCDMA

Android基础知识(6)—数据持久化之数据存储

阅读前,请浏览此处上方目录. Android基础知识(6)-数据持久化之数据存储 本章内容为个人笔记,参考书籍有:<疯狂的android>第3版.<第一行代码> 首先,我们要知道什么是数据持久化. 数据持久化就是指那些内存中的瞬时数据保存到存储设备中,保证即使手机在关机的情况下,这些数据不会丢失.保存在内存中的数据是处于瞬时状态,保存在存储设备中的数据是处于持久状态.持久化技术则是提供了一种机制可以让数据在瞬时状态和持久状态之间进行转换. Android系统主要提供了三种方式用于简