如何在Recyclerview中添加和减去数量?

时间:2017-12-23 11:39:03

标签: android cart product-quantity

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));

            }
        });

https://ibb.co/cQst76

https://ibb.co/edZ9Em

我尝试了很多东西但却无法理解为什么代码不能用于递增和递减数量。我在stackoverflow中看到了很多问题,但没有一个在android studio中。顺便说一句,我使用firebase作为我的后端。 此代码段来自Recyclerview适配器onbindviewholder方法。

1 个答案:

答案 0 :(得分:0)

你可能需要制作product_quantity&amp; productArrayList作为类的公共变量。我不确定你是如何定义它的,因为你的代码没有显示出来。