【该项目实训是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;@Overrideprotected 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);//初始化的内部TabinitInnerTabHost();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);}@Overridepublic 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;}@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", "当前点击列表"+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" ><TabHostandroid:id="@+id/innerTab_discount"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:layout_alignParentTop="true" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="wrap_content" ></TabWidget><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayoutandroid:id="@+id/innerTab_discount_tab1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ListViewandroid:id="@+id/promotionFoodList"android:layout_width="match_parent"android:layout_height="match_parent"></ListView></LinearLayout><LinearLayoutandroid:id="@+id/innerTab_discount_tab2"android:layout_width="match_parent"android:orientation="vertical"android:layout_height="match_parent" ><ListViewandroid: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; //餐馆IDprivate 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;}@Overridepublic 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;}@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);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><pre code_snippet_id="563089" snippet_file_name="blog_20141226_11_5643964" name="code" class="java"><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" ><ImageViewandroid:id="@+id/foodlist_item_img"android:layout_width="60dp"android:layout_height="60dp"android:background="@drawable/imgbg"android:src="@drawable/food_02" /><TextViewandroid: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" /><TextViewandroid: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" /><TextViewandroid: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" /><TextViewandroid: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" /><TextViewandroid: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" /><TextViewandroid: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基础知识【项目实训-实现二级导航“今日活动”及读取数据库】【5】相关推荐

  1. 机电一体化基础知识及实训QY-JDYT01

    A.机电一体化:是在机械的主功能.动力功能.信息功能和控制功能上引进微电子技术,并将机械装置与电子装置用相关软件有机结合而构成系统的总称. ※B.机电一体化系统:按照机电一体化方法设计出来的机械与电子 ...

  2. 数字信号处理与高频电路基础知识与实训QY-MS300E

    数字信号处理与高频电路 1.FS FT DFS DTFT DFT 的联系和区别 时域上任意连续的周期信号可以分解为无限多个正弦信号之和,在频域上就表示为离散非周期的信号,即时域连续周期对应频域离散非周 ...

  3. python在线投票系统讲解_Python开发基础-项目实训-在线投票系统ppt课件

    <Python开发基础-项目实训-在线投票系统ppt课件>由会员分享,可在线阅读,更多相关<Python开发基础-项目实训-在线投票系统ppt课件(27页珍藏版)>请在人人文库 ...

  4. python实训项目-Python开发基础-项目实训-在线投票系统.pptx

    项目实训-在线投票系统本章任务/30完成"在线投票系统"添加投票候选人删除候选人为候选人投票按序号投票删除投票输出统计信息--本章目标/30理解程序的基本概念会使用顺序.选择.循环 ...

  5. 计算机电路基础知识教程ppt课件,计算机电路基础教程与实训1.ppt

    计算机电路基础教程与实训1 第1章 基础电子元器件介绍 教学提示与教学要求 1.1 电阻器 1.2 电容器 1.3 电感器 1.4 半导体二极管 1.5 半导体三极管 1.6 本章小结 1.7 习题一 ...

  6. python在线投票系统_Python开发基础-项目实训-在线投票系统.pptx

    项目实训-在线投票系统本章任务/30完成"在线投票系统"添加投票候选人删除候选人为候选人投票按序号投票删除投票输出统计信息--本章目标/30理解程序的基本概念会使用顺序.选择.循环 ...

  7. python在线投票系统源码-Python开发基础-项目实训-在线投票系统.pptx

    项目实训-在线投票系统本章任务/30完成"在线投票系统"添加投票候选人删除候选人为候选人投票按序号投票删除投票输出统计信息--本章目标/30理解程序的基本概念会使用顺序.选择.循环 ...

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

    BeagleBone Black项目实训手册(大学霸内部资料) 介绍:本教程是<BeagleBone Black快速入门教程>的后续教程.本教程以项目操作为主,讲解LED项目.声音项目.传 ...

  9. java质数和合数的程序_《java项目实训》课程设计计算器.doc

    <java项目实训>课程设计计算器.doc 课程设计报告课程名称JAVA项目实训课程设计设计名称基于JAVA计算器的设计与实现学生学号学生姓名学生学号学生姓名学生学号学生姓名学生学号学生姓 ...

最新文章

  1. MonoScene: 单目3D语义场景补全
  2. API Monitor(API监控工具)
  3. 窗口之间值、控件的传递
  4. 实战|渗透学校某内网服务器
  5. Servlet 与 Ajax 交互一直报status=parsererror
  6. Apache 重写规则的常见应用 (rewrite)
  7. 前端学习(3058):vue+element今日头条管理-回顾
  8. Google的十个核心技术
  9. 软件工程革命 三部曲 —— 前传
  10. BZOJ3527 推出卷积公式FFT求值
  11. 萌新做点小玩意儿DAY-2 五子棋AI拓展思想
  12. java 插件开发教程_Eclipse插件开发的详细教程
  13. 最全的权限系统设计方案(图解)
  14. 计算机毕业设计SSM常见病辅助食疗系统【附源码数据库】
  15. 加拿大计算机硕士gpa不够,[加拿大硕士留学gpa不够怎么算]加拿大硕士留学,GPA不够怎么办...
  16. 计算机毕业设计java+ssm酒店管理系统(源码+系统+mysql数据库+Lw文档)
  17. PYTHON实践——GUI界面
  18. 图解蓝牙 BR/EDR 和BLE的区别
  19. 瓦伦达心态是什么瓦伦达心态在投资中应用
  20. Excel黑科技——Sum、indirect、Row的使用

热门文章

  1. 执行this.$destory()指令后,原生DOM也没有响应的问题
  2. 万网空间 php伪静态,百度云虚拟主机zblogphp在Nginx环境下设置伪静态规则,极为重要...
  3. docker exec -it container1 /bin/bash 异常
  4. 什么是主力的入资异常介入点?如何捕捉主力入资?
  5. ARM到底是冯诺依曼结构还是哈佛结构
  6. 微软的“胡萝卜”会比“大棒”更有效吗
  7. Codeforces gym 2013-2014 Samara SAU ACM ICPC Quarterfinal Qualification Contest
  8. java 修改mysql数据库表结构_MYSQL数据库表结构优化方法详解
  9. 控制成本,控制成本知识点,挣值和实际成本、EAC,ETC.TCPI解析表
  10. 正则表达式元字符查询