Android ListView条目内增减按钮修改对应商品数量实现方法咨询
要实现ListView条目内增减按钮修改对应数量,只需要按以下步骤调整即可,核心逻辑是给数据源新增数量存储字段,自定义Adapter绑定按钮点击事件:
1. 调整数据源,新增商品数量字段
原有代码中HashMap只存储了商品名、价格、图片,需要新增count字段存储每个商品的购买数量,修改加载JSON的循环逻辑:
try { JSONObject obj=new JSONObject(LoadFromJsonAssets()); Resources resources = context.getResources(); JSONArray array =obj.getJSONArray("domestic"); HashMap<String,String> list; ArrayList<HashMap<String,String>> arrayList=new ArrayList<>(); for (int i=0;i<array.length();i++){ JSONObject o=array.getJSONObject(i); String productName=o.getString("productName"); String productPrice=o.getString("productPrice"); String productPic=o.getString("productPic"); final int resourceId = resources.getIdentifier(productPic, "drawable", context.getPackageName()); list= new HashMap<>(); list.put("productName",productName); list.put("productPrice",productPrice); list.put("productPic",Integer.toString(resourceId) ); // 新增数量字段,默认0 list.put("count", "0"); arrayList.add(list); } // 这里替换为自定义Adapter,后面会给出实现 final CustomListAdapter adapter = new CustomListAdapter(this, arrayList); listView.setAdapter(adapter); } catch (Exception e) { e.printStackTrace(); }
2. 自定义ListView适配类,绑定按钮点击事件
新建CustomListAdapter.java文件,继承BaseAdapter,在getView方法中绑定每个条目的增减按钮点击事件,修改对应位置的数量后刷新列表:
public class CustomListAdapter extends BaseAdapter { private Context context; private ArrayList<HashMap<String, String>> dataList; public CustomListAdapter(Context context, ArrayList<HashMap<String, String>> dataList) { this.context = context; this.dataList = dataList; } @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return dataList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.list_view_design, parent, false); holder = new ViewHolder(); holder.productPic = convertView.findViewById(R.id.productPic); holder.productName = convertView.findViewById(R.id.productName); holder.productPrice = convertView.findViewById(R.id.productPrice); holder.decreaseBtn = convertView.findViewById(R.id.decreasing); holder.countTv = convertView.findViewById(R.id.counterrrr); holder.increaseBtn = convertView.findViewById(R.id.Increasing); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } HashMap<String, String> item = dataList.get(position); holder.productName.setText(item.get("productName")); holder.productPrice.setText(item.get("productPrice")); holder.productPic.setImageResource(Integer.parseInt(item.get("productPic"))); holder.countTv.setText(item.get("count")); // 给按钮设置当前位置tag,避免列表复用时点击事件错位 holder.increaseBtn.setTag(position); holder.decreaseBtn.setTag(position); // 增加按钮点击事件 holder.increaseBtn.setOnClickListener(v -> { int pos = (int) v.getTag(); HashMap<String, String> curItem = dataList.get(pos); int count = Integer.parseInt(curItem.get("count")); count++; curItem.put("count", String.valueOf(count)); notifyDataSetChanged(); }); // 减少按钮点击事件 holder.decreaseBtn.setOnClickListener(v -> { int pos = (int) v.getTag(); HashMap<String, String> curItem = dataList.get(pos); int count = Integer.parseInt(curItem.get("count")); if (count > 0) { count--; curItem.put("count", String.valueOf(count)); notifyDataSetChanged(); } }); return convertView; } // 缓存控件,避免重复执行findViewById提升性能 static class ViewHolder { ImageView productPic; TextView productName; TextView productPrice; ImageView decreaseBtn; TextView countTv; ImageView increaseBtn; } }
3. 效果参考

内容的提问来源于stack exchange,提问作者Muratcan Yıldız
相关产品推荐
相关产品推荐

