Parama Sudha
Parama Sudha

Reputation: 2623

Cart Count increment & decrement while clicking button

I populates cart items into the list using cursor adapter. My ListView item has

  1. Price TextView
  2. Product Name TextView
  3. Count TextView
  4. Increment Button
  5. Decrement Buttton

When I click the increment / decrement button the value is incremented/ decremented in a certain condition of AFTER TOUCHING THE LISTVIEW ROW,otherwise the count is not changed. In some case, if I click the 1st row increment button ,item count is incremented in 2nd row.

NOTE: 1. I am populating List using Cursor Adapter. 2. Using Increment & Decrement clicklistener in activity class.

Here is my code:

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;

                    }
                }
            }
        });
    }
});

Upvotes: 0

Views: 4294

Answers (1)

Jas
Jas

Reputation: 3212

You can use setTag() and getTag() methods for saving the counter value in each list item.

Refer this answer for more details.

Upvotes: 1

Related Questions