1.临时保存每个item中的显示数量控件的text:
HashMap<Integer,Interger> map = new HashMap<Integer,Integer>();
使用map来保存每条item显示的数量值。EditText在BaseAdapter中的getView()
方法中去显示对应position的map中的值,如下:
editText.setText(map.get(position));
2. "-"和"+"添加点击监听去操作map数据。使用接口回调的方法,然后再adapter.notifyDataSetChanged()
---> 调用baseadapter中的getView()方法。
代码如下:
BaseAdapter
@Override public View getView(final int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.item_food, null); holder = new ViewHolder(); holder.img = (ImageView) convertView .findViewById(R.id.product_list_item_img); holder.name = (TextView) convertView .findViewById(R.id.product_list_item_name); holder.price = (TextView) convertView .findViewById(R.id.product_list_item_price); holder.description_long = (TextView) convertView .findViewById(R.id.product_list_item_product_long); holder.layout_foodCountSelect = (LinearLayout) convertView .findViewById(R.id.layout_food_count_select); holder.btn_food_delete = (Button) convertView .findViewById(R.id.btn_food_delete); holder.btn_food_count = (Button) convertView .findViewById(R.id.btn_food_count); holder.btn_food_add = (Button) convertView .findViewById(R.id.btn_food_add); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } Product product = datalist.get(position); if (product != null) { if (!TextUtils.isEmpty(product.getName())) { holder.name.setText(product.getName()); } holder.price.setText("¥" + product.getPrice()); if (!TextUtils.isEmpty(product.getDescription())) { holder.description_long .setText("介绍" + product.getDescription()); } } holder.layout_foodCountSelect .setVisibility(mIsBulkPurchase ? View.VISIBLE : View.GONE); convertView.setOnClickListener(new ClickListener(product)); holder.btn_food_count.setText(foodCountMap.get(position) + ""); holder.btn_food_delete.setOnClickListener(new ClickListener(position)); holder.btn_food_add.setOnClickListener(new ClickListener(position)); return convertView; } class ViewHolder { private ImageView img; // 产品图片 private TextView name; // 名称 private TextView price; // 价格 private TextView description_long; // 介绍:长的 private LinearLayout layout_foodCountSelect; private Button btn_food_delete; private Button btn_food_count; private Button btn_food_add; } /** * 设置进入批量选购模式 */ public void setBulkPurchase(boolean isShow) { mIsBulkPurchase = isShow; } public boolean getBulkPurchase() { return mIsBulkPurchase; } public void setFoodCountMap(int position, boolean isAdd) { int foodCount = foodCountMap.get(position); if (foodCount == 0 && !isAdd) return; foodCountMap.put(position, isAdd ? (foodCount + 1) : (foodCount - 1)); } private class ClickListener implements OnClickListener { ViewHolder mViewHolder; Product mProduct; int mPosition; public ClickListener(Product product) { this.mProduct = product; } public ClickListener(int position) { this.mPosition = position; } @Override public void onClick(View v) { int foodCount = Integer .parseInt((String) ((Button) holder.btn_food_count) .getText()); switch (v.getId()) { default: Intent intent = new Intent(); intent.setClass(context, FoodDetail_Activity.class); context.startActivity(intent); break; case R.id.btn_food_delete: mCallback.dealFoodCount(mPosition, false); break; case R.id.btn_food_add: mCallback.dealFoodCount(mPosition, true); break; } } } public interface FoodCallback { // 处理美食份数选择事件 void dealFoodCount(int pos, boolean isAdd); } public void setCallback(FoodCallback callback) { this.mCallback = callback; }
Fragment:
@Override public void dealFoodCount(int position, boolean isAdd) { // TODO Auto-generated method stub adapter.setFoodCountMap(position,isAdd); adapter.notifyDataSetChanged(); mMainActivity.setSelectedFoodMoney(getSelectedFoodMoney()); }
时间: 2024-10-11 07:13:06