我對購物車中單件商品的總價更新錯誤。如果我增加數(shù)量并減少數(shù)量,則會發(fā)生以下情況:增加數(shù)量1=4,10;2=4,10;3=8,20;4=12,30;減少數(shù)量:從 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貢獻(xiàn)1942條經(jīng)驗 獲得超3個贊
可能,問題出在代碼序列中,您應(yīng)該輸入以下代碼:
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
提交
取消