【发布时间】:2017-12-23 11:39:02
【问题描述】:
product_quantity = productArrayList.get(position).getQuantity();
holder.display_quantity.setText(String.valueOf(product_quantity));
// holder.selected_quantity.setText("Selected Quantity: " + String.valueOf(productArrayList.get(position).getQuantity()));
/*
* When the select quantity button is pressed in the Cart Fragment via this activity( that is the recyclerview )
* it shows a dialog which allows the user to change the quantity. The quantity interaction has been done in the CartQuantityActivity.
* */
holder.btn_add_quantity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
product_quantity++;
if (product_quantity > 4) {
product_quantity = 4;
}
cartRef.child(cartlistKeys.get(position)).child("quantity").setValue(product_quantity);
productArrayList.get(position).setQuantity(product_quantity);
holder.display_quantity.setText(String.valueOf(product_quantity));
}
});
holder.btn_subtract_quantity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
product_quantity--;
if (product_quantity < 1) {
product_quantity = 1;
}
cartRef.child(cartlistKeys.get(position)).child("quantity").setValue(product_quantity);
productArrayList.get(position).getQuantity();
holder.display_quantity.setText(String.valueOf(product_quantity));
}
});
我尝试了很多东西,但不明白为什么代码不能用于增加和减少数量。我在 stackoverflow 中看到了很多问题,但在 android studio 中都没有。顺便说一句,我使用 firebase 作为我的后端。 这段代码 sn -p 来自 Recyclerview 适配器的 onbindviewholder 方法。
【问题讨论】:
-
您检查过
cartListkeys.get(position)的值吗?也尝试“addOnCompleteListener”检查问题 -
将您的代码发布为 text。 代码图像完全没用。
-
我已经以图像的形式提供了所有数据,因为我在 stackoverflow 中遇到了文本格式问题。
-
每次按下按钮时,请不要创建另一个变量,而是通过获取数量值进行更改并通知适配器。
标签: android cart product-quantity