我对购物车中单件商品的总价更新错误。如果我增加数量并减少数量,则会发生以下情况:
增加数量1=4,10;2=4,10;3=8,20;4=12,30;减少数量:从 4(16,30) 到 3 = 16,40;2 = 12,30;1=8,20;
但所有物品的总价格是正确的。
public class CartAdapter extends RecyclerView.Adapter<CartViewHolder> {
private List<Order> listData = new ArrayList<>();
private Cart cart;
public CartAdapter(List<Order> listData, Cart cart) {
this.listData = listData;
this.cart = cart;
}
@NonNull
@Override
public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(cart);
View itemView = inflater.inflate(R.layout.cart_layout, parent, false);
return new CartViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final CartViewHolder holder, final int position) {
holder.btn_quantity.setNumber(listData.get(position).getQuantity());
holder.btn_quantity.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
@Override
public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
Locale locale = new Locale("de", "DE");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
double price = (Double.parseDouble(listData.get(position).getPrice()))*(Double.parseDouble(listData.get(position).getQuantity()));
//int price = (Integer.parseInt(listData.get(position).getPrice()))*(Integer.parseInt(listData.get(position).getQuantity()));
holder.txt_price.setText(fmt.format(price));
holder.txt_cart_name.setText(listData.get(position).getProductName());
}
});
手掌心
相关分类