最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Android实现简单购物车功能
时间:2022-06-25 23:16:05 编辑:袖梨 来源:一聚教程网
本文实例为大家分享了Android实现购物车功能的具体代码,供大家参考,具体内容如下
MainActivity布局:
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> goodsList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initDate(); } //控制价格展示 private void priceControl(Map pitchOnMap){ totalCount = 0; totalPrice = 0.00; for(int i=0;i 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 map){ List > waitDeleteList=new ArrayList<>(); Map waitDeleteMap =new HashMap<>(); for(int i=0;i 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 (); for(int i=0;i<10;i++){ HashMap 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布局:
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> dataList; private ViewHolder vh; private Map pitchOnMap; private RefreshPriceInterface refreshPriceInterface; public CartAdapter(Context context, List > list){ this.context=context; this.dataList=list; pitchOnMap=new HashMap<>(); for(int i=0;i 0){ if(pitchOnMap.get(dataList.get(position).get("id"))==0)vh.checkBox.setChecked(false); else vh.checkBox.setChecked(true); HashMap 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 getPitchOnMap(){ return pitchOnMap; } public void setPitchOnMap(Map pitchOnMap){ this.pitchOnMap=new HashMap<>(); this.pitchOnMap.putAll(pitchOnMap); } public interface RefreshPriceInterface{ void refreshPrice(Map 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; } }
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22