我对购物车中单件商品的总价更新错误。如果我增加数量并减少数量,则会发生以下情况:增加数量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@Overridepublic 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);}@Overridepublic 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()); } });
1 回答
手掌心
TA贡献1942条经验 获得超3个赞
可能,问题出在代码序列中,您应该输入以下代码:
Order order = listData.get(position);
order.setQuantity(String.valueOf(newValue));
new Database(cart).updateCart(order);
在开始:
public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
// event listener code
}
这个块。
我希望它有帮助:)
添加回答
举报
0/150
提交
取消