购物车数量增量&单击按钮时减少

时间:2016-03-03 05:32:06

标签: android listview

我使用游标适配器将购物车项目填充到列表中。我的ListView项目有

  1. Price TextView
  2. 产品名称TextView
  3. 计算TextView
  4. 增量按钮
  5. 减少Buttton
  6. 当我单击增量/减量按钮时,值在触摸LISTVIEW ROW 的某个条件下递增/递减,否则计数不会改变。   在某些情况下,如果我单击第1行增量按钮,则项目计数在第2行递增。

    注意: 1.我使用Cursor Adapter填充List。 2.使用增量&在活动类中减少clicklistener。

    这是我的代码:

    cartList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(final AdapterView<?> parent, View itemView, final int position, final long rowId) {
    
            txt_cartProduct = (TextView) itemView.findViewById(R.id.cartProduct);
            txt_cartQuantity = (TextView) itemView.findViewById(R.id.cartQuantity);
            txt_cartPrice = (TextView) itemView.findViewById(R.id.cartPrice);
            txt_cartPriceDum = (TextView) itemView.findViewById(R.id.cartPriceDum);
            txt_cartCount = (TextView) itemView.findViewById(R.id.cartCount);
            img_ivDecrease = (ImageView) itemView.findViewById(R.id.ivDecrease);
            img_ivIncrease = (ImageView) itemView.findViewById(R.id.ivIncrease);
            but_addTowish = (Button) itemView.findViewById(R.id.addTowish);
            but_remove = (Button) itemView.findViewById(R.id.remove);
    
            img_ivIncrease.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    strPrice = txt_cartPrice.getText().toString();
                    price = Integer.parseInt(strPrice);
                    int counter = 0;
                    try {
                        counter = Integer.parseInt(txt_cartCount.getText().toString());
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                    counter++;
    
                    if (counter > 0) {
                        txt_cartCount.setText(Integer.toString(counter));
                        txt_cartPrice.setVisibility(View.GONE);
                        txt_cartPriceDum.setVisibility(View.VISIBLE);
                        quantity = txt_cartCount.getText().toString();
    
                        total = (Integer.parseInt(quantity)) * (price);
                        netA = String.valueOf(total);
                        sum += price;
                        if (sum == 0) {
                            netAmount = Integer.parseInt(txt_cartPriceDum.getText().toString());
                        }
                        netAmount = sum;
                        Log.e("Sum is", String.valueOf(sum));
                        txt_cartPriceDum.setText(String.valueOf(total));
                        cartCount = Integer.parseInt(quantity);
    
                        Toast.makeText(context, "netAmount" + netAmount + "\n" + "Total" + total, Toast.LENGTH_SHORT).show();
                        if (counter == 1) {
                            cartPrice = price;
                            cartSum = sum;
                        }
    
                        if (counter == 0) {
                            cartPrice = 0;
                            cartSum = 0;
                            cartCount = 0;
                            Toast.makeText(context, "Minimum Item is 1", Toast.LENGTH_SHORT).show();
                        }
                        int count_check = 1;
                        if (count_check >= position) {
                            count_check++;
                        } else if (count_check == 0) {
                            Toast.makeText(context, "Minimum Item is 1", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
    
    
            img_ivDecrease.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    strPrice = txt_cartPrice.getText().toString();
                    price = Integer.parseInt(strPrice);
                    int counter = 0;
                    try {
                        counter = Integer.parseInt(txt_cartCount.getText().toString());
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                    counter--;
    
                    if (counter > 0) {
                        txt_cartCount.setText(Integer.toString(counter));
                        txt_cartPrice.setVisibility(View.GONE);
                        txt_cartPriceDum.setVisibility(View.VISIBLE);
                        quantity = txt_cartCount.getText().toString();
                        total = (Integer.parseInt(quantity)) * (price);
                        netA = String.valueOf(total);
                        sum -= price;
                        if (sum == 0) {
                            netAmount = Integer.parseInt(txt_cartPriceDum.getText().toString());
                        }
                        Log.e("Sum - is", String.valueOf(sum));
                        txt_cartPriceDum.setText(String.valueOf(total));
                        cartCount = Integer.parseInt(quantity);
                        Toast.makeText(context, "netAmount" + netAmount + "\n" + "Total" + total, Toast.LENGTH_SHORT).show();
                        if (counter == 1) {
                            cartPrice = price;
                            cartSum = sum;
                        }
    
                        if (counter == 0) {
                            cartPrice = 0;
                            cartSum = 0;
                            cartCount = 0;
    
                        }
                    }
                }
            });
        }
    });
    

1 个答案:

答案 0 :(得分:1)

您可以使用setTag()getTag()方法在每个列表项中保存计数器值。

有关详细信息,请参阅this answer