android ListView的item中Button(購物數量加減器)

來源:互聯網
上載者:User

標籤:android listview

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

@Overridepublic 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;}@Overridepublic 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:

@Overridepublic void dealFoodCount(int position, boolean isAdd) {// TODO Auto-generated method stubadapter.setFoodCountMap(position,isAdd);adapter.notifyDataSetChanged();mMainActivity.setSelectedFoodMoney(getSelectedFoodMoney());}



android ListView的item中Button(購物數量加減器)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.