Android实现简单购物车功能
程序员文章站
2023-12-12 17:35:22
本文实例为大家分享了android实现购物车功能的具体代码,供大家参考,具体内容如下
mainactivity布局:
本文实例为大家分享了android实现购物车功能的具体代码,供大家参考,具体内容如下
mainactivity布局:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <linearlayout android:id="@+id/top_bar" android:layout_width="match_parent" android:layout_height="48dp" android:background="#e24146" android:orientation="vertical" > <textview 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" /> </linearlayout> <listview android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:childindicator="@null" android:groupindicator="@null" > </listview> <linearlayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" > <linearlayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2.5" android:gravity="center_vertical" android:orientation="horizontal" > <checkbox 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:button="@drawable/check_box_bg" android:checkmark="?android:attr/listchoiceindicatormultiple" android:gravity="center" android:minheight="64dp" android:paddingleft="10dp" android:textappearance="?android:attr/textappearancelarge" android:visibility="visible" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:text="合计:" android:textsize="16sp" android:textstyle="bold" /> <textview android:id="@+id/tv_total_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥0.00" android:textcolor="@color/purple" android:textsize="16sp" android:textstyle="bold" /> </linearlayout> <textview android:id="@+id/tv_delete" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/orange" android:clickable="true" android:gravity="center" android:text="删除" android:textcolor="#fafafa" /> <textview 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" /> </linearlayout> </linearlayout>
import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.checkbox; import android.widget.listview; import android.widget.textview; import android.widget.toast; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import java.util.list; import java.util.map; import java.util.random; public class mainactivity extends appcompatactivity implements cartadapter.refreshpriceinterface ,view.onclicklistener{ private listview listview; private checkbox cb_check_all; private textview tv_total_price; private textview tv_delete; private textview tv_go_to_pay; private cartadapter adapter; private double totalprice = 0.00; private int totalcount = 0; private list<hashmap<string,string>> goodslist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initdate(); } //控制价格展示 private void pricecontrol(map<string, integer> pitchonmap){ totalcount = 0; totalprice = 0.00; for(int i=0;i<goodslist.size();i++){ if(pitchonmap.get(goodslist.get(i).get("id"))==1){ totalcount=totalcount+integer.valueof(goodslist.get(i).get("count")); double goodsprice=integer.valueof(goodslist.get(i).get("count"))*double.valueof(goodslist.get(i).get("price")); totalprice=totalprice+goodsprice; } } tv_total_price.settext("¥ "+totalprice); tv_go_to_pay.settext("付款("+totalcount+")"); } @override public void refreshprice(map<string, integer> pitchonmap) { pricecontrol(pitchonmap); } @override public void onclick(view view) { switch (view.getid()){ case r.id.all_chekbox: alltheselected(); break; case r.id.tv_go_to_pay: if(totalcount<=0){ toast.maketext(this,"请选择要付款的商品~",toast.length_short).show(); return; } toast.maketext(this,"钱就是另一回事了~",toast.length_short).show(); break; case r.id.tv_delete: if(totalcount<=0){ toast.maketext(this,"请选择要删除的商品~",toast.length_short).show(); return; } checkdelete(adapter.getpitchonmap()); break; } } //删除操作 private void checkdelete(map<string,integer> map){ list<hashmap<string,string>> waitdeletelist=new arraylist<>(); map<string,integer> waitdeletemap =new hashmap<>(); for(int i=0;i<goodslist.size();i++){ if(map.get(goodslist.get(i).get("id"))==1){ waitdeletelist.add(goodslist.get(i)); waitdeletemap.put(goodslist.get(i).get("id"),map.get(goodslist.get(i).get("id"))); } } goodslist.removeall(waitdeletelist); map.remove(waitdeletemap); pricecontrol(map); adapter.notifydatasetchanged(); } //全选或反选 private void alltheselected(){ map<string,integer> map=adapter.getpitchonmap(); boolean ischeck=false; boolean isuncheck=false; iterator iter = map.entryset().iterator(); while (iter.hasnext()) { map.entry entry = (map.entry) iter.next(); if(integer.valueof(entry.getvalue().tostring())==1)ischeck=true; else isuncheck=true; } if(ischeck==true&&isuncheck==false){//已经全选,做反选 for(int i=0;i<goodslist.size();i++){ map.put(goodslist.get(i).get("id"),0); } cb_check_all.setchecked(false); }else if(ischeck==true && isuncheck==true){//部分选择,做全选 for(int i=0;i<goodslist.size();i++){ map.put(goodslist.get(i).get("id"),1); } cb_check_all.setchecked(true); }else if(ischeck==false && isuncheck==true){//一个没选,做全选 for(int i=0;i<goodslist.size();i++){ map.put(goodslist.get(i).get("id"),1); } cb_check_all.setchecked(true); } pricecontrol(map); adapter.setpitchonmap(map); adapter.notifydatasetchanged(); } private void initview(){ listview = (listview) findviewbyid(r.id.listview); cb_check_all = (checkbox) findviewbyid(r.id.all_chekbox); tv_total_price = (textview) findviewbyid(r.id.tv_total_price); tv_delete = (textview) findviewbyid(r.id.tv_delete); tv_go_to_pay = (textview) findviewbyid(r.id.tv_go_to_pay); tv_go_to_pay.setonclicklistener(this); tv_delete.setonclicklistener(this); cb_check_all.setonclicklistener(this); adapter=new cartadapter(this,goodslist); adapter.setrefreshpriceinterface(this); listview.setadapter(adapter); adapter.notifydatasetchanged(); } private void initdate(){ goodslist=new arraylist<>(); for(int i=0;i<10;i++){ hashmap<string,string> map=new hashmap<>(); map.put("id",(new random().nextint(10000)%(10000-2900+2900) + 2900)+""); map.put("name","购物车里的第"+(i+1)+"件商品"); map.put("type",(i+20)+"码"); map.put("price",(new random().nextint(100)%(100-29+29) + 29)+""); map.put("count",(new random().nextint(10)%(10-1+1) + 1)+""); goodslist.add(map); } initview(); } }
cartadapter布局:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <view android:layout_width="match_parent" android:layout_height="1dp" android:background="#cccccc" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/page_backgroup" android:orientation="horizontal" > <checkbox 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:button="@drawable/check_box_bg" android:checkmark="?android:attr/listchoiceindicatormultiple" android:gravity="center" android:minheight="64dp" android:minwidth="32dp" android:textappearance="?android:attr/textappearancelarge" android:visibility="visible" /> <imageview 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/good_icon" /> <relativelayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_margintop="10dp" android:layout_marginleft="13dp" > <textview 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:textcolor="@color/grey_color1" android:textsize="14sp" /> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_marginbottom="30dp" android:orientation="horizontal" > <textview android:id="@+id/tv_goods_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:singleline="true" android:textcolor="@color/orange_color" android:textsize="14sp" android:textstyle="bold" /> <textview 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:textcolor="@color/grey_color3" android:textsize="10sp"/> <linearlayout 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" > <textview android:id="@+id/tv_reduce" android:layout_width="25dp" android:layout_height="25dp" android:background="@drawable/text_angle_gray" android:gravity="center" android:text="一" android:textcolor="@color/grey_color1" android:textsize="12sp" /> <textview android:id="@+id/tv_num" android:layout_width="25dp" android:layout_height="25dp" android:background="@drawable/text_angle" android:gravity="center" android:singleline="true" android:text="1" android:textcolor="@color/grey_color1" android:textsize="12sp" /> <textview android:id="@+id/tv_add" android:layout_width="25dp" android:layout_height="25dp" android:background="@drawable/text_angle_right" android:gravity="center" android:text="+" android:textcolor="@color/grey_color1" android:textsize="12sp" /> </linearlayout> </relativelayout> </relativelayout> </linearlayout> </linearlayout>
import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.checkbox; import android.widget.imageview; import android.widget.textview; import android.widget.toast; import java.util.hashmap; import java.util.list; import java.util.map; /** * created by lipeng * 2017/6/5. */ public class cartadapter extends baseadapter { private context context; private list<hashmap<string,string>> datalist; private viewholder vh; private map<string,integer> pitchonmap; private refreshpriceinterface refreshpriceinterface; public cartadapter(context context, list<hashmap<string,string>> list){ this.context=context; this.datalist=list; pitchonmap=new hashmap<>(); for(int i=0;i<datalist.size();i++){ pitchonmap.put(datalist.get(i).get("id"),0); } } @override public view getview(final int position, view view, viewgroup viewgroup) { vh=new viewholder(); if(view==null){ view= layoutinflater.from(context).inflate(r.layout.item_layout,null); vh.checkbox=(checkbox)view.findviewbyid(r.id.check_box); vh.icon=(imageview)view.findviewbyid(r.id.iv_adapter_list_pic); vh.name=(textview)view.findviewbyid(r.id.tv_goods_name); vh.price=(textview)view.findviewbyid(r.id.tv_goods_price); vh.type=(textview)view.findviewbyid(r.id.tv_type_size); vh.num=(textview)view.findviewbyid(r.id.tv_num); vh.reduce=(textview)view.findviewbyid(r.id.tv_reduce); vh.add=(textview)view.findviewbyid(r.id.tv_add); view.settag(vh); }else { vh=(viewholder)view.gettag(); } if(datalist.size()>0){ if(pitchonmap.get(datalist.get(position).get("id"))==0)vh.checkbox.setchecked(false); else vh.checkbox.setchecked(true); hashmap<string,string> map=datalist.get(position); vh.name.settext(map.get("name")); vh.num.settext(map.get("count")); vh.type.settext(map.get("type")); vh.price.settext("¥ "+(double.valueof(map.get("price")) * integer.valueof(map.get("count")))); vh.checkbox.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { final int index=position; if(((checkbox)view).ischecked())pitchonmap.put(datalist.get(index).get("id"),1);else pitchonmap.put(datalist.get(index).get("id"),0); refreshpriceinterface.refreshprice(pitchonmap); } }); vh.reduce.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { final int index=position; datalist.get(index).put("count",(integer.valueof(datalist.get(index).get("count"))-1)+""); if(integer.valueof(datalist.get(index).get("count"))<=0){ //可提示是否删除该商品,确定就remove,否则就保留1 string deid=datalist.get(index).get("id"); datalist.remove(index); pitchonmap.remove(deid); } notifydatasetchanged(); refreshpriceinterface.refreshprice(pitchonmap); } }); vh.add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { final int index=position; datalist.get(index).put("count",(integer.valueof(datalist.get(index).get("count"))+1)+""); if(integer.valueof(datalist.get(index).get("count"))>15){ //15为库存可选择上限 toast.maketext(context,"已达库存上限~",toast.length_short).show(); return; } notifydatasetchanged(); refreshpriceinterface.refreshprice(pitchonmap); } }); } return view; } public map<string,integer> getpitchonmap(){ return pitchonmap; } public void setpitchonmap(map<string,integer> pitchonmap){ this.pitchonmap=new hashmap<>(); this.pitchonmap.putall(pitchonmap); } public interface refreshpriceinterface{ void refreshprice(map<string, integer> pitchonmap); } public void setrefreshpriceinterface(refreshpriceinterface refreshpriceinterface){ this.refreshpriceinterface=refreshpriceinterface; } @override public object getitem(int i) { return null; } @override public long getitemid(int i) { return 0; } @override public int getcount() { if (datalist != null) { return datalist.size(); } else { return 0; } } class viewholder{ checkbox checkbox; imageview icon; textview name,price,num,type,reduce,add; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。