Search code examples
androidlistviewandroid-recyclerviewandroid-listviewandroid-adapter

How to update a texview in main screen by changing a value in an item inside a recycleview?


I want to develop a screen that represents a store and in it you can find a list of products (presented by a recycleview) where you can find the price of each item and select your desired quantity.

when the user update the quantity of the product the price changes too.

I have a textview below the recycleview which display the total sum of prices of all selected products.

When the user want to change his required quantity of a specific product, it is simple to update the price represented in the item but how to update in the same time the one (the total one) in the main screen ?


Solution

  • Use Interface to listen to the change in the recycler view item and update the value on the user select the quantity. Create a interface class

    public interface UpdateListener {
        void onQuantityUpdated();
    
    }
    

    In your adapter class

     private Context context;
    private UpdateListener deleteListener;
    
    
    public YourRVAdapter(Context context, List<ConsumedMaterialModel> list, 
        UpdateListener updateListener) {
        this.context = context;
        this.list = list;
        this.deleteListener = deleteListener;
    }
    

    on user select quantity inside adapter

    updateListener.onQuantityUpdated();
    

    Now implement this interface in your Activity

    public class MainActivity extends AppCompatActivity implements UpdateListener
    
    
     @Override
    public void onQuantityUpdated() {
        //update your textview by calculating amount here
    }