Android开发Android开发经验谈Android技术知识

扣丁学堂解析Android Studio实现简单购物车功能及源码

2018-07-12  本文已影响56人  994d14631d16

  今天扣丁学堂Android培训给大家分享了AndroidStudio实现简单购物车功能及源码,下面我们一起来看一下吧。

请输入图片描述

  MainActivity的布局文件

  xmlns:app="http://schemas.android.com/apk/res-auto"

  xmlns:tools="http://schemas.android.com/tools"

  android:layout_width="match_parent"

  android:layout_height="match_parent"

  android:orientation="vertical"

  tools:context=".MainActivity">

  android:id="@+id/top_bar"

  android:layout_width="match_parent"

  android:layout_height="48dp"

  android:background="#E24146"

  android:orientation="vertical">

  android:id="@+id/title"

  android:layout_width="match_parent"

  android:layout_height="wrap_content"

  android:gravity="center"

  android:minHeight="48dp"

  android:text="购物车"

  android:textColor="#ffffff"

  android:textSize="17sp"/>

  android:id="@+id/listview"

  android:layout_width="match_parent"

  android:layout_height="0dp"

  android:layout_weight="1"

  android:childIndicator="@null"

  android:groupIndicator="@null">

  android:layout_width="match_parent"

  android:layout_height="50dp"

  android:orientation="horizontal">

  android:layout_width="0dp"

  android:layout_height="match_parent"

  android:layout_weight="2.5"

  android:gravity="center_vertical"

  android:orientation="horizontal">

  android:id="@+id/all_chekbox"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_gravity="center_vertical"

  android:layout_marginLeft="10dp"

  android:layout_marginRight="4dp"

  android:checkMark="?android:attr/listChoiceIndicatorMultiple"

  android:gravity="center"

  android:minHeight="64dp"

  android:paddingLeft="10dp"

  android:textAppearance="?android:attr/textAppearanceLarge"

  android:visibility="visible"/>

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_marginLeft="5dp"

  android:text="合计:"

  android:textSize="16sp"

  android:textStyle="bold"/>

  android:id="@+id/tv_total_price"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:text="¥0.00"

  android:textSize="16sp"

  android:textStyle="bold"/>

  android:id="@+id/tv_delete"

  android:layout_width="0dp"

  android:layout_height="match_parent"

  android:layout_weight="1"

  android:clickable="true"

  android:background="#a29e9e"

  android:gravity="center"

  android:text="删除"

  android:textColor="#FAFAFA"/>

  android:id="@+id/tv_go_to_pay"

  android:layout_width="0dp"

  android:layout_height="match_parent"

  android:layout_weight="1"

  android:background="#E24146"

  android:clickable="true"

  android:gravity="center"

  android:text="付款(0)"

  android:textColor="#FAFAFA"/>

  条目的布局文件

  android:layout_width="match_parent"

  android:layout_height="match_parent"

  android:orientation="vertical">

  android:layout_width="match_parent"

  android:layout_height="1dp"

  android:background="#CCCCCC"/>

  android:layout_width="match_parent"

  android:layout_height="wrap_content"

  android:orientation="horizontal">

  android:id="@+id/check_box"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_gravity="center_vertical"

  android:layout_marginLeft="10dp"

  android:layout_marginRight="4dp"

  android:checkMark="?android:attr/listChoiceIndicatorMultiple"

  android:gravity="center"

  android:minHeight="64dp"

  android:minWidth="32dp"

  android:textAppearance="?android:attr/textAppearanceLarge"

  android:visibility="visible"/>

  android:id="@+id/iv_adapter_list_pic"

  android:layout_width="85dp"

  android:layout_height="85dp"

  android:layout_marginBottom="15dp"

  android:layout_marginTop="13dp"

  android:scaleType="centerCrop"

  android:src="@mipmap/ic_launcher"

  />

  android:layout_width="wrap_content"

  android:layout_height="match_parent"

  android:layout_gravity="center_vertical"

  android:layout_marginTop="10dp"

  android:layout_marginLeft="13dp">

  android:id="@+id/tv_goods_name"

  android:layout_width="match_parent"

  android:layout_height="wrap_content"

  android:layout_marginRight="10dp"

  android:layout_marginTop="20dp"

  android:ellipsize="end"

  android:maxLines="2"

  android:text="商品"

  android:textSize="14sp"/>

  android:layout_width="match_parent"

  android:layout_height="wrap_content"

  android:layout_alignParentBottom="true"

  android:layout_marginBottom="30dp"

  android:orientation="horizontal">

  android:id="@+id/tv_goods_price"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_centerVertical="true"

  android:singleLine="true"

  android:textSize="14sp"

  android:textStyle="bold"

  android:text="价格"/>

  android:id="@+id/tv_type_size"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_centerVertical="true"

  android:layout_marginLeft="10dp"

  android:layout_toRightOf="@+id/tv_goods_price"

  android:singleLine="true"

  android:textSize="10sp"/>

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_alignParentRight="true"

  android:layout_centerVertical="true"

  android:layout_marginRight="15dp"

  android:orientation="horizontal">

  android:id="@+id/tv_reduce"

  android:layout_width="25dp"

  android:layout_height="25dp"

  android:gravity="center"

  android:background="#ccc"

  android:text="一"

  android:textSize="12sp"/>

  android:id="@+id/tv_num"

  android:layout_width="25dp"

  android:layout_height="25dp"

  android:gravity="center"

  android:singleLine="true"

  android:text="1"

  android:textSize="12sp"/>

  android:id="@+id/tv_add"

  android:layout_width="25dp"

  android:layout_height="25dp"

  android:gravity="center"

  android:text="十"

  android:background="#ccc"

  android:textSize="12sp"/>

  CartAdapter适配器

  publicclassCaetAdapterextendsBaseAdapter{

  privateContextcontext;

  privateList>list;

  privateHashMappitchOnMap;

  publicHashMapgetPitchOnMap(){

  returnpitchOnMap;

  }

  publicvoidsetPitchOnMap(HashMappitchOnMap){

  this.pitchOnMap=pitchOnMap;

  }

  publicCaetAdapter(Contextcontext,List>list){

  this.context=context;

  this.list=list;

  pitchOnMap=newHashMap<>();

  for(inti=0;i

  pitchOnMap.put(list.get(i).get("id"),0);

  }

  }

  @Override

  publicintgetCount(){

  returnlist.size();

  }

  @Override

  publicObjectgetItem(intposition){

  returnlist.get(position);

  }

  @Override

  publiclonggetItemId(intposition){

  returnposition;

  }

  @Override

  publicViewgetView(finalintposition,ViewconvertView,ViewGroupparent){

  convertView=View.inflate(context,R.layout.item_layout,null);

  finalCheckBoxcheckBox;

  ImageViewicon;

  finalTextViewname,price,num,type,reduce,add;

  checkBox=convertView.findViewById(R.id.check_box);

  icon=convertView.findViewById(R.id.iv_adapter_list_pic);

  name=convertView.findViewById(R.id.tv_goods_name);

  price=convertView.findViewById(R.id.tv_goods_price);

  type=convertView.findViewById(R.id.tv_type_size);

  num=convertView.findViewById(R.id.tv_num);

  reduce=convertView.findViewById(R.id.tv_reduce);

  add=convertView.findViewById(R.id.tv_add);

  name.setText(list.get(position).get("name"));

  price.setText("¥"+(Integer.valueOf(list.get(position).get("price")))*(Integer.valueOf(list.get(position).get("count"))));

  type.setText(list.get(position).get("type"));

  num.setText(list.get(position).get("count"));

  if(pitchOnMap.get(list.get(position).get("id"))==0){

  checkBox.setChecked(false);

  }else{

  checkBox.setChecked(true);

  }

  checkBox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){

  @Override

  publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){

  if(checkBox.isChecked()){

  pitchOnMap.put(list.get(position).get("id"),1);

  }else{

  pitchOnMap.put(list.get(position).get("id"),0);

  }

  mrefreshPriceInterface.refreshPrice(pitchOnMap);

  }

  });

  //商品数量减

  reduce.setOnClickListener(newView.OnClickListener(){

  @Override

  publicvoidonClick(Viewv){

  if(Integer.valueOf(list.get(position).get("count"))<=1){

  Toast.makeText(context,"数量不能再减啦,只能删除!",Toast.LENGTH_SHORT).show();

  }else{

  list.get(position).put("count",(Integer.valueOf(list.get(position).get("count"))-1)+"");

  notifyDataSetChanged();

  }

  mrefreshPriceInterface.refreshPrice(pitchOnMap);

  }

  });

  //商品数量加

  add.setOnClickListener(newView.OnClickListener(){

  @Override

  publicvoidonClick(Viewv){

  list.get(position).put("count",(Integer.valueOf(list.get(position).get("count"))+1)+"");

  notifyDataSetChanged();

  mrefreshPriceInterface.refreshPrice(pitchOnMap);

  }

  });

  returnconvertView;

  }

  /**

  *创建接口

  */

  publicinterfaceRefreshPriceInterface{

  /**

  *把价格展示到总价上

  *@parampitchOnMap

  */

  voidrefreshPrice(HashMappitchOnMap);

  }

  /**

  *定义一个接口对象

  */

  privateRefreshPriceInterfacemrefreshPriceInterface;

  /**

  *向外部暴露一个方法

  *把价格展示到总价上

  *@paramrefreshPriceInterface

  */

  publicvoidsetRefreshPriceInterface(RefreshPriceInterfacerefreshPriceInterface){

  mrefreshPriceInterface=refreshPriceInterface;

  }

  }

  MainActivity

  publicclassMainActivityextendsAppCompatActivityimplementsView.OnClickListener,CaetAdapter.RefreshPriceInterface{

  privateLinearLayouttop_bar;

  privateListViewlistview;

  privateCheckBoxall_chekbox;

  privateTextViewprice;

  privateTextViewdelete;

  privateTextViewtv_go_to_pay;

  privateListgoodsList;

  privateUserDaouserDao;

  privateList>listmap=newArrayList<>();

  privateCaetAdapteradapter;

  privatedoubletotalPrice=0.00;

  privateinttotalCount=0;

  @Override

  protectedvoidonCreate(BundlesavedInstanceState){

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  initView();

  }

  privatevoidinitView(){

  top_bar=(LinearLayout)findViewById(R.id.top_bar);

  listview=(ListView)findViewById(R.id.listview);

  all_chekbox=(CheckBox)findViewById(R.id.all_chekbox);

  price=(TextView)findViewById(R.id.tv_total_price);

  delete=(TextView)findViewById(R.id.tv_delete);

  tv_go_to_pay=(TextView)findViewById(R.id.tv_go_to_pay);

  all_chekbox.setOnClickListener(this);

  delete.setOnClickListener(this);

  tv_go_to_pay.setOnClickListener(this);

  initDate();

  adapter=newCaetAdapter(MainActivity.this,listmap);

  listview.setAdapter(adapter);

  adapter.setRefreshPriceInterface(this);

  }

  @Override

  publicvoidonClick(Viewv){

  switch(v.getId()){

  caseR.id.all_chekbox:

  AllTheSelected();

  break;

  caseR.id.tv_delete:

  checkDelete(adapter.getPitchOnMap());

  break;

  caseR.id.tv_go_to_pay:

  if(totalCount<=0){

  Toast.makeText(this,"请选择要付款的商品~",Toast.LENGTH_SHORT).show();

  }else{

  Toast.makeText(this,"付款成功",Toast.LENGTH_SHORT).show();

  }

  break;

  }

  }

  /**

  *数据

  */

  privatevoidinitDate(){

  //创建集合

  goodsList=newArrayList<>();

  //数据库

  userDao=Myapplication.getInstances().getDaoSession().getUserDao();

  userDao.deleteAll();

  //数据源

  for(inti=0;i<10;i++){

  //向数据库存放数据

  Useruser=newUser((long)i,

  "购物车里的第"+(i+1)+"件商品",

  (i+20)+"码",

  "10",

  "10");

  userDao.insert(user);

  }

  //从数据库中把数据放到集合中

  goodsList=userDao.loadAll();

  //把结合中的数据放到HashMap集合中

  for(inti=0;i

  HashMapmap=newHashMap<>();

  map.put("id",goodsList.get(i).getId()+"");

  map.put("name",goodsList.get(i).getName());

  map.put("type",(goodsList.get(i).getType()));

  map.put("price",goodsList.get(i).getPrice()+"");

  map.put("count",goodsList.get(i).getCount()+"");

  listmap.add(map);

  }

  }

  @Override

  publicvoidrefreshPrice(HashMappitchOnMap){

  priceControl(pitchOnMap);

  }

  /**

  *控制价格展示总价

  */

  privatevoidpriceControl(MappitchOnMap){

  totalCount=0;

  totalPrice=0.00;

  for(inti=0;i

  if(pitchOnMap.get(listmap.get(i).get("id"))==1){

  totalCount=totalCount+Integer.valueOf(listmap.get(i).get("count"));

  doublegoodsPrice=Integer.valueOf(listmap.get(i).get("count"))*Double.valueOf(listmap.get(i).get("price"));

  totalPrice=totalPrice+goodsPrice;

  }

  }

  price.setText("¥"+totalPrice);

  tv_go_to_pay.setText("付款("+totalCount+")");

  }

  /**

  *删除控制价格展示总价

  *@parammap

  */

  privatevoidcheckDelete(Mapmap){

  List>waitDeleteList=newArrayList<>();

  MapwaitDeleteMap=newHashMap<>();

  for(inti=0;i

  if(map.get(listmap.get(i).get("id"))==1){

  waitDeleteList.add(listmap.get(i));

  waitDeleteMap.put(listmap.get(i).get("id"),map.get(listmap.get(i).get("id")));

  }

  }

  listmap.removeAll(waitDeleteList);

  map.remove(waitDeleteMap);

  priceControl(map);

  adapter.notifyDataSetChanged();

  }

  /**

  *全选或反选

  */

  privatevoidAllTheSelected(){

  HashMapmap=adapter.getPitchOnMap();

  booleanisCheck=false;

  booleanisUnCheck=false;

  Iteratoriter=map.entrySet().iterator();

  while(iter.hasNext()){

  Map.Entryentry=(Map.Entry)iter.next();

  if(Integer.valueOf(entry.getValue().toString())==1){

  isCheck=true;

  }else{

  isUnCheck=true;

  }

  }

  if(isCheck==true&&isUnCheck==false){//已经全选,做反选

  for(inti=0;i

  map.put(listmap.get(i).get("id"),0);

  }

  all_chekbox.setChecked(false);

  }elseif(isCheck==true&&isUnCheck==true){//部分选择,做全选

  for(inti=0;i

  map.put(listmap.get(i).get("id"),1);

  }

  all_chekbox.setChecked(true);

  }elseif(isCheck==false&&isUnCheck==true){//一个没选,做全选

  for(inti=0;i

  map.put(listmap.get(i).get("id"),1);

  }

  all_chekbox.setChecked(true);

  }

  priceControl(map);

  adapter.setPitchOnMap(map);

  adapter.notifyDataSetChanged();

  }

  }

  以上就是扣丁学堂Android培训之AndroidStudio实现简单购物车功能及源码的详细介绍,希望本文的内容对学习Android开发费有深入的了解,如果你想学习更多的Android知识,对Android有全面的了解,请关注扣丁学堂微信公众号,或登录扣丁学堂官网了解更多,还有大量Android视频教程等着你来观看。

上一篇下一篇

猜你喜欢

热点阅读