购物车项目计数增量/减量&购物车价格总和

时间:2016-03-13 10:21:24

标签: android android-cursoradapter

我需要增加/减少购物车产品。增加/减少价值。但是从这里我需要得到我所有购物车产品的总和。有一些计算错误。所以请帮我解决。

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;

                    }
                }
            }
        });
    }
});

0 个答案:

没有答案
相关问题