Android基础知识【项目实训-添加购物车】【8】
来源:互联网 发布:知乎土壤取样 国家标准 编辑:程序博客网 时间:2024/05/17 23:17
【该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材】
【项目题目】:校园订餐App设计
综合案例
【目标】当用户点击某一个 餐品时,会打开详细介绍界面,并可以确定购买数量,点击“预订”按钮就可以加入购物车。购物车中的每一项都对应 将来生成订单的
1、食品详情界面对应FoodDetailActivity 类,当点击食品列表中的某一项时,打开这个界面,并把对应的 食品对象通过Intent传过来。
下面这是传递所选食物对象的方法:
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);
下面这是实现详情界面的代码:
public class FoodDetailActivity extends Activity {
FoodInfo food;//食物详情界面,展示的的食物对象EditText num;//购物份数Button buyBt;//购买按钮,ImageButton addBt,subBt;//增加和减少 物品按钮@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_food_detail);//getActionBar().setDisplayHomeAsUpEnabled(true);getActionBar().setDisplayShowHomeEnabled(true);//获取intentIntent intent=getIntent();food=(FoodInfo) intent.getSerializableExtra("food");if(food==null){finish();}init(food);//num=(EditText) findViewById(R.id.ac_fd_acount);buyBt =(Button) findViewById(R.id.ac_fd_buyBt);addBt =(ImageButton) findViewById(R.id.ac_fd_acount_add);subBt =(ImageButton) findViewById(R.id.ac_fd_acount_sub);addBt.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {String n=num.getText().toString();num.setText(String.valueOf(Integer.parseInt(n)+1));}});subBt.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {String n=num.getText().toString();int nn=Integer.parseInt(n)-1;num.setText(String.valueOf(nn<1?1:nn));}});buyBt.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {String count=num.getText().toString();if(count==null||count.trim().length()<1)return;creatOrderItem(food,Integer.parseInt(count));}});//Log.i("Msg", food.toString());}/**产生订单项对象 * @param food2 所订食物 * @param acount 数量 */protected void creatOrderItem(FoodInfo food2, int acount) {//Log.i("Msg", "产生订单项");AccountItem item =new AccountItem();//订单项所属订单,在用户提交订单时 生成。item.setCount(acount);item.setFood(food2);item.setFoodId(food2.get_id());EatApp app =(EatApp) getApplication();app.orderItems.add(item);Toast.makeText(this, "订购"+food.getFoodName()+"成功", Toast.LENGTH_LONG).show();Log.i("Msg", "购车车中数据"+app.orderItems.size());}//根据传入的 食物对象,初始化本界面private void init(FoodInfo food) {ImageView iv=(ImageView) findViewById(R.id.ac_fd_img);iv.setImageResource(Integer.parseInt( food.getImgId()));TextView des=(TextView) findViewById(R.id.ac_fd_des);des.setText(food.getDescription());TextView name=(TextView) findViewById(R.id.ac_fd_name);name.setText(food.getFoodName());TextView price=(TextView) findViewById(R.id.ac_fd_price);price.setText("¥ "+food.getPrice());TextView tag1=(TextView) findViewById(R.id.ac_fd_tag1);tag1.setText(food.getCategory());TextView tag2=(TextView) findViewById(R.id.ac_fd_tag2);tag2.setText(food.getType());}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.//getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch(item.getItemId()){case android.R.id.home://回退按钮点击Intent intent =new Intent(this,MainActivity.class);startActivity(intent);finish();break;}return super.onOptionsItemSelected(item);}}2、食物详情界面布局文件,文件名是 activity_food_detail.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=".FoodDetailActivity" > <ImageView android:id="@+id/ac_fd_img" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerCrop" android:src="@drawable/food_05" /> <TextView android:id="@+id/ac_fd_des" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/ac_fd_img" android:padding="10dp" android:text="好吃就多来点……" /> <TextView android:layout_width="fill_parent" android:layout_height="3dp" android:layout_below="@id/ac_fd_des" android:background="#993333" /> <TextView android:id="@+id/ac_fd_nameStr" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ac_fd_des" android:padding="5dp" android:text="食品名称:" android:textSize="20sp" /> <TextView android:id="@+id/ac_fd_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/ac_fd_nameStr" android:layout_alignBottom="@id/ac_fd_nameStr" android:padding="5dp" android:text="烤牛肉" android:textSize="16sp" /> <TextView android:id="@+id/ac_fd_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/ac_fd_name" android:layout_alignBottom="@id/ac_fd_name" android:padding="5dp" android:text="¥25.00" android:textSize="16sp" /><TextView android:id="@+id/ac_fd_tagStr" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ac_fd_nameStr" android:padding="5dp" android:text="标签:" android:textSize="20sp" /><LinearLayout android:id="@+id/ac_fd_tagBox" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/ac_fd_tagStr" android:orientation="horizontal" > <TextView android:id="@+id/ac_fd_tag1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/textbg" android:gravity="center_horizontal" android:text="中餐" android:textSize="15sp" android:textColor="#DDDDDD" /> <TextView android:id="@+id/ac_fd_tag2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/textbg" android:gravity="center_horizontal" android:textSize="15sp" android:textColor="#DDDDDD" android:text="微辣"/> <TextView android:id="@+id/ac_fd_tag3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/textbg" android:gravity="center_horizontal" android:text="咸淡适中" android:textSize="15sp" android:textColor="#DDDDDD" /></LinearLayout><TextView android:id="@+id/ac_fd_orderStr" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ac_fd_tagBox" android:padding="5dp" android:text="预定数量:" android:textSize="20sp" /><LinearLayout android:id="@+id/ac_fd_acountBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/ac_fd_orderStr" android:gravity="center_horizontal|center_vertical" ><ImageButton android:id="@+id/ac_fd_acount_sub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:src="@drawable/sub" /> <EditText android:id="@+id/ac_fd_acount" android:layout_width="50dp" android:layout_height="30dp" android:singleLine="true" android:inputType="number" android:text="1" android:gravity="center_horizontal" android:textColor="#DDDDDD" android:background="@drawable/textbg" /> <ImageButton android:id="@+id/ac_fd_acount_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:src="@drawable/add" /> </LinearLayout><Button android:id="@+id/ac_fd_buyBt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="15dp"android:background="@drawable/buybtstyle" android:textColor="#DDDDDD" android:text="加入预定" /></RelativeLayout>
3、食品详情界面效果
可以增加或减少 预订数量,也可以直接输入预订数量。
点击预订,可以加入购物车,并提示。
经 餐品加入购物车中,采用如下代码实现。
EatApp app =(EatApp) getApplication();app.orderItems.add(item);实质上是将 购物的物品和数量组成 订单项对象,然后加入共享集合 区间。
4、当购物车非空时,点击购物车菜单,进入购物车详情界面。购物车界面对应ShopcartActivity.java。这个界面的功能也稍微有些复杂,因为它需要能更改 购物车中的数量和 删除某个项目。
/** * @author Administrator * 显示购物车中 详细信息界面 */public class ShopcartActivity extends Activity {//统计费用TextView yuanshi,peican,gongji;//提交账单Button submit;//购物车货品列表ListView shopList;ShopcartListAdapter sla;//ListView 项触屏时用int x1,y1,x2,y2;//点下 和抬起时的坐标//应用对象EatApp app;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_shopcart);app=(EatApp) getApplication();getActionBar().setDisplayHomeAsUpEnabled(true);yuanshi=(TextView) findViewById(R.id.shopcart_total_yuanjia);peican=(TextView) findViewById(R.id.shopcart_total_peisongfei);gongji=(TextView) findViewById(R.id.shopcart_total_gongji);submit=(Button) findViewById(R.id.shopcart_submit);submit.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {//验证登录if(app.userInfo==null){Toast.makeText(ShopcartActivity.this, "请先登录", Toast.LENGTH_LONG).show();//创建登录对话框new LoginDialog(ShopcartActivity.this);return;}else{Account a =createAccount();if(a.get_id()==null){Toast.makeText(ShopcartActivity.this,"订单创建失败",Toast.LENGTH_SHORT).show();}else{Toast.makeText(ShopcartActivity.this,"订单编号:"+a.get_id(),Toast.LENGTH_SHORT).show();Log.i("Msg", "订单已经创建:"+a.toString());//提交订单成功,返回主页或支付页面Intent intent =new Intent(ShopcartActivity.this,PayActivity.class);startActivity(intent);ShopcartActivity.this.finish();}}}});//初始化购物列表shopList =(ListView) findViewById(R.id.shopcart_listview);initLv();<span style="background-color: rgb(255, 153, 255);">shopList.setOnTouchListener(new OnTouchListener(){@Overridepublic boolean onTouch(View v, MotionEvent me) {if(me.getAction()==MotionEvent.ACTION_DOWN){x1=(int) me.getX();y1=(int) me.getY();}else if(me.getAction()==MotionEvent.ACTION_UP){x2=(int) me.getX();y2=(int) me.getY();//点下和抬起时,对于的列表项int p1=((ListView)v).pointToPosition(x1, y1);int p2=((ListView)v).pointToPosition(x2, y2);if(p1==p2&&x1-x2>20){Log.i("Msg", "打算删除"+p1);if(p1<0)return false;removeItem(((ListView)v).getChildAt(p1),p1);//app.orderItems.remove(p1);//移除数据//initLv();//重新初始化//updateCheck();//更新价格}}return true;}});</span>}//产生订单public Account createAccount(){EatApp app =(EatApp) ShopcartActivity.this.getApplication();Account a =new Account();a.setState(0);a.setUserId(app.userInfo.get_id());a.setCreateDate(DateUtil.getCurrentDate());//将订单插入数据库EatDbHelper dbh=new EatDbHelper(this, "fooddb.db3", null, 1); Account account =new AccountDao().createAccount(dbh,a);//订单成功后,需要将订单项插入数据库if(account.get_id()!=null){int maxid =new AccountItemDao().insertAccountItem(dbh, account, app.orderItems);if(maxid<1)account.set_id(null);else{//订单项及订单都插入成功,清空购物车app.orderItems.clear();}Log.i("Msg", "订单项插入最大元素为:"+maxid);}return account;}//移除列表中的指定项,并播放动画protected void removeItem(View itemView, final int pos) {final Animation an=AnimationUtils.loadAnimation(this, R.anim.slidetoleft);an.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation arg0) {}@Overridepublic void onAnimationRepeat(Animation arg0) {}@Overridepublic void onAnimationEnd(Animation arg0) {app.orderItems.remove(pos);//移除数据updateCheck();sla.notifyDataSetChanged();//an.cancel();}});itemView.startAnimation(an);}//统计购物车中物品,计算总价public void updateCheck() {List<AccountItem> items=app.orderItems;float my=0;for (AccountItem accountItem : items) {my+=accountItem.getCount()*Float.parseFloat(accountItem.getFood().getPrice())*Float.parseFloat(accountItem.getFood().getDiscount());}my=Math.round(my*100/100);yuanshi.setText("¥"+my);float pc =0.00f;//配餐费在系统中并未 启用peican.setText("¥"+String.valueOf(pc));gongji.setText("¥"+String.valueOf(my+pc));}//初始化购物车列表private void initLv() {sla=new ShopcartListAdapter(this,app.orderItems,R.layout.shopcartlist_item); shopList.setAdapter(sla);updateCheck();//初始化列表数据后,计算账单}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch(item.getItemId()){case android.R.id.home://回退按钮点击Intent intent =new Intent(this,MainActivity.class);startActivity(intent);finish();break;}return super.onOptionsItemSelected(item);}}在购物车界面上要完成这样几个功能:
(0)检验是否登录,如果没有登录,打开登录对话框
(1)统计购物车中的物品,总计费用是多少
(2)当购物车中的物品数量改变事,重新计算 总价
(3)向左滑动某个项目时,可以删除掉它
(4)点击提交订单时,产生订单,更新数据库,同时将购物车中的订单项,补充所属订单属性,并跳转到支付界面
5、购物车的布局文件
<LinearLayout 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" android:orientation="vertical" tools:context=".ShopcartActivity" > <ListView android:id="@+id/shopcart_listview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </ListView> <LinearLayout android:id="@+id/shopcart_bottombox" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#99000000" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical" > <ImageView android:id="@+id/shopcart_img_yuanjia" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/sale_02" android:scaleType="centerCrop" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="原始总价:" android:textSize="16sp" android:textColor="@color/shopcart_fontcolor" /> <TextView android:id="@+id/shopcart_total_yuanjia" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥26.50" android:textColor="@color/shopcart_fontcolor" /> <ImageView android:id="@+id/shopcart_img_peisongfei" android:layout_width="25dp" android:layout_height="25dp" android:src="@drawable/sale_03" android:layout_marginLeft="8dp" android:scaleType="centerCrop" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="配餐费:" android:textColor="@color/shopcart_fontcolor" android:textSize="16sp" /> <TextView android:id="@+id/shopcart_total_peisongfei" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥0.00" android:textColor="@color/shopcart_fontcolor" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="2dp" android:background="#AAAAAA" android:orientation="horizontal" ></LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical" > <ImageView android:id="@+id/shopcart_img_gongji" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/sale_04" android:scaleType="centerCrop" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账单共计:" android:textStyle="bold" android:textColor="@color/shopcart_fontcolor_total" android:textSize="16sp" /> <TextView android:id="@+id/shopcart_total_gongji" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥25.00" android:textStyle="bold" android:textColor="@color/shopcart_fontcolor_total" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="2dp" android:background="#EEEEEE" android:orientation="horizontal" ></LinearLayout> <Button android:id="@+id/shopcart_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交账单" android:background="@drawable/buybtstyle" /> </LinearLayout></LinearLayout>
6、购物车界面效果
7、初始化购物车列表的适配器是自定义的。
ShopcartListAdapter代码如下:
public class ShopcartListAdapter extends BaseAdapter {ShopcartActivity context;List<AccountItem> data;int layout;public ShopcartListAdapter(ShopcartActivity context, List<AccountItem> data,int layout) {super();this.context = context;this.data = data;this.layout = layout;}@Overridepublic int getCount() {return data.size();}@Overridepublic Object getItem(int arg0) {return data.get(arg0);}@Overridepublic long getItemId(int arg0) {return arg0;}@Overridepublic View getView(final int index, View view, ViewGroup group) {View v=LayoutInflater.from(context).inflate(layout, null);ImageView iv =(ImageView) v.findViewById(R.id.shopcartlist_item_img);iv.setImageResource(Integer.parseInt(data.get(index).getFood().getImgId()));TextView name=(TextView) v.findViewById(R.id.shopcartlist_item_name);name.setText(data.get(index).getFood().getFoodName());TextView price=(TextView) v.findViewById(R.id.shopcartlist_item_price);price.setText("单价:¥"+data.get(index).getFood().getPrice());float disc=Float.parseFloat(data.get(index).getFood().getDiscount());if(disc<1){TextView discount=(TextView) v.findViewById(R.id.shopcartlist_item_discount);discount.setText("折扣:"+data.get(index).getFood().getDiscount());}final EditText count=(EditText) v.findViewById(R.id.shopcartlist_item_acount);count.setText(String.valueOf(data.get(index).getCount()));<span style="background-color: rgb(255, 102, 102);">//监听 数量修改ImageButton sub=(ImageButton) v.findViewById(R.id.shopcartlist_item_sub);ImageButton add=(ImageButton) v.findViewById(R.id.shopcartlist_item_add);sub.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {int n =Integer.parseInt(count.getText().toString());n--;if(n<1)n=1;count.setText(String.valueOf(n));data.get(index).setCount(n);context.updateCheck();}});add.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {int n=Integer.parseInt(count.getText().toString())+1;count.setText(String.valueOf(n));data.get(index).setCount(n);context.updateCheck();}});</span>return v;}}
这个适配器中,主要是对 增加 和减少按钮的 功能实现
//监听 数量修改ImageButton sub=(ImageButton) v.findViewById(R.id.shopcartlist_item_sub);ImageButton add=(ImageButton) v.findViewById(R.id.shopcartlist_item_add);8、购物车列表项的布局文件是shopcartlist_item。xml。
<?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" android:descendantFocusability="blocksDescendants" android:padding="4dp" > <ImageView android:id="@+id/shopcartlist_item_img" android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/food_02" android:scaleType="centerCrop" /><TextView android:id="@+id/shopcartlist_item_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="大盘鸡" android:textSize="16sp" android:layout_toRightOf="@id/shopcartlist_item_img" android:layout_marginLeft="5dp" /><TextView android:id="@+id/shopcartlist_item_price" android:layout_width="wrap_content" android:layout_height="wrap_content"android:text=""android:layout_below="@id/shopcartlist_item_name"android:layout_alignLeft="@id/shopcartlist_item_name"android:layout_marginTop="5dp" /><TextView android:id="@+id/shopcartlist_item_discount" android:layout_width="wrap_content" android:layout_height="wrap_content"android:text=""android:layout_marginLeft="10dp"android:layout_toRightOf="@id/shopcartlist_item_price"android:layout_alignBottom="@id/shopcartlist_item_price" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#CCCCCC" android:orientation="horizontal" android:gravity="center_horizontal" android:layout_alignParentRight="true" android:padding="4dp" > <ImageButton android:id="@+id/shopcartlist_item_sub" android:layout_width="24dp" android:layout_height="24dp" android:src="@drawable/sub" android:scaleType="centerCrop" /> <EditText android:id="@+id/shopcartlist_item_acount" android:layout_width="40dp" android:gravity="center_horizontal" android:layout_height="wrap_content" android:background="@drawable/textbg" /> <ImageButton android:id="@+id/shopcartlist_item_add" android:layout_width="24dp" android:layout_height="24dp" android:src="@drawable/add" android:scaleType="centerCrop" /> </LinearLayout></RelativeLayout>9、向左滑动某一列表,可以将其移除,此处用一个动画,采用属性动画文件完成。文件名是slidetoleft.xml。
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="800" android:fromXDelta="0" android:toXDelta="-100%" > </translate>这个动画的播放,由触屏监听实现。施加触屏操作的代码 背景为 粉色。
9 1
- Android基础知识【项目实训-添加购物车】【8】
- android添加购物车动画实现
- android添加购物车动画实现
- android 添加到购物车动画
- 添加购物&查看购物车
- Android基础知识【项目实训-功能设计】【1】
- 快速开发Android购物车项目
- 添加到购物车
- 添加购物车动画
- 添加购物车功能
- 添加购物车
- 购物车添加删除
- 添加购物车
- 添加购物车
- Android_添加购物车
- 添加购物车
- 添加购物车_购物车
- Android中使用动画动态添加商品进购物车
- 笔记: 试用Kibana+Logstash+Elasticsearch+Redis
- 关于linux内核无法编译成模块的解决方法
- AndroidAnnotations开源框架
- 谷歌 中文版开发文档
- 1066. Root of AVL Tree (25)
- Android基础知识【项目实训-添加购物车】【8】
- Socket.IO聊天室~简单实用
- log4j配置祥解
- 初识go语言
- C/C++中的void和void*
- 实现Django网站列表自动分页
- linux下scp命令详解--主机之间拷贝文件
- Java String之StringBuilder以及它的API的详细使用方法(2)
- 阿里不遗余力阻击微信 胜算几何? 转载