简体   繁体   中英

android recyclerview restoring state in object

The app functionality is similar to shopping cart. User can add products and increase/decrease quantity by clicking on respective buttons. By default the quantity value is set to 1. I am facing a problem whenever the user adds new product to recyclerview the previous products quantity is also changing to 1. How to avoid this? How can I restore the state(incremented/decremented value) in object?

 @Override
public void onBindViewHolder(@NonNull RVOrderBrdAdapter.RVViewHolder holder, int position) {
    //getting the product of the specified position
    OrderBrdDataModel product = filteredList.get(position);

    //binding the data with the viewholder views
    double org_p = product.getBrdprice();
    holder.brd_value.setText(String.valueOf(product.getBrdprice()));
    holder.brd_product.setText(product.getBrdname());
    holder.brd_quantity.setText("1");

    holder.brd_increment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            number = Integer.parseInt(""+holder.brd_quantity.getText().toString());
            if(holder.brd_quantity.getText().toString().equals("")){
                Toast.makeText(v.getContext(), "Quantity cannot be empty", Toast.LENGTH_SHORT).show();
            }else if(number >=9999){
                Toast.makeText(v.getContext(), "Quantity cannot be greater than 9999", Toast.LENGTH_SHORT).show();

            }else{
                number++;
            }

            holder.brd_quantity.setText(""+number);
            double iw = Double.parseDouble(String.format("%.2f",(org_p*number)));
            holder.brd_value.setText(String.valueOf (iw));
        }
    });

    holder.brd_decrement.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            number = Integer.parseInt(""+holder.brd_quantity.getText().toString());
            if(holder.brd_quantity.getText().toString().equals("")){
                Toast.makeText(v.getContext(), "Quantity cannot be empty", Toast.LENGTH_SHORT).show();
            }else if(number<=0){
                Toast.makeText(v.getContext(), "Quantity cannot be less than 0", Toast.LENGTH_SHORT).show();
                number = 0;
            }else {
                number--;

            }
            holder.brd_quantity.setText(""+number);
            double dw = Double.parseDouble(String.format("%.2f",(org_p*number)));
            holder.brd_value.setText(String.valueOf(dw));
        }
    });

You could add the quantity to the OrderBrdDataModel or wrap OrderBrdDataModel in something like OrderBrdDataModelWithQuantity. In OnBindViewHolder use the quantity from this variable and make sure to update the quantity value in brd_increment/decrement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM