I need to get Total Value of Cart Products.Here I am using Recyclerview & Bean Class for get & set the listview items. I have tried to get.But while click increment / decrement button the getting values are not the correct value.
Let consider if my cart Product item value is Rs 100/- & Initial cart count as 1.
When I click increment button I am getting 200, 500, 900
When I click decrement button I am getting, 600, 400, 300
Here is my code.Please Check it.
Adapter Class:
holder.ivIncrease.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (null != mListener) {
cartRes.get(position).CartCount = (bigInt + 1);
notifyItemChanged(position);
mListener.onListFragmentInteraction(holder.cartRess);
}
getIncreaseTotal();
}
});
holder.ivDecrease.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (null != mListener) {
cartRes.get(position).CartCount = (bigInt - 1);
notifyItemChanged(position);
mListener.onListFragmentInteraction(holder.cartRess);
}
getDecreaseTotal();
}
});
private void getDecreaseTotal() {
for (int i = 0; i < cartRes.size() ; i++)
{
CartRes cartBasket = cartRes.get(i);
System.out.println(cartBasket);
totalPrice = cartBasket.getQuantity() * cartBasket.getPrice();
sum -= totalPrice;
Log.e("Total Sum", String.valueOf(sum));
}
}
public void getIncreaseTotal() {
for (int i = 0; i < cartRes.size() ; i++)
{
CartRes cartBasket = cartRes.get(i);
System.out.println(cartBasket);
totalPrice = cartBasket.getQuantity() * cartBasket.getPrice();
sum += totalPrice;
Log.e("Total Sum", String.valueOf(sum));
}
}
BeanClass:
public class CartRes {
public String CartcategoryName;
public String CartproductName;
public String product_id;
public int CartPrice;
public int CartCount;
private int Quantity;
public CartRes(String product_idd, String productName, String categoryName, int cartCount, int cartPrice) {
this.product_id = product_idd;
this.CartproductName = productName;
this.CartcategoryName = categoryName;
this.CartCount = cartCount;
this.CartPrice = cartPrice;
}
public CartRes() {
}
public int getPrice() {
return CartPrice;
}
public int getQuantity() {
return CartCount;
}
}
Your problem may occur in your
totalPrice = cartBasket.getQuantity() * cartBasket.getPrice();
statement.
For example, If there is 1 product in your cart with price 200 and its quantity is also 1. So your total = 200
. Then you changed its quantity from 1 to 2.And what your doing is adding +1 to your quantity, so quantity became 2.By then you are multiplying its price 200 with quantity 2 so you are suppose to wrong here because you have already 200 added for 1 quantity in your "sum" variable.You have to only multiply it by 1 quantity and then add it into your "sum" variable.I think same happens in your decrement method too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.