Search code examples
androidandroid-recyclerviewnestedrecyclerview

Making Cart in Android - Refresh not working properly


I'm trying to make a cart like in grofers.

Expected: When I add items to cart by clicking on add button, go to other page and come back. The items which are in cart should have the quantity selected and other items should have add button with them.

Actual: When I add items to cart by clicking on add button, go to other page and come back.

  • All items have the add button whether they are in cart or aren't in cart
  • On pressing add button under one of the item, all items in the cart get's the quantity(something that should have happened at the load of the page is happening now on clicking)

Problem:

  • Items aren't updating in RecyclerView when fragment opens
  • Nested RecyclerView not working on fragment open

Can someone help to solve this please? Thankyou


Solution

  • There's a simple trick to achieve this!

    You can create a getter setter class with an temp array. And declare it has static in the adapter class. So whenever you are adding the quantity add that in the temp list too and everytime while setting up data in bind method check if that item exist in the temp list. If it's there than set the quantity from that temp list. If there's no I'd present in list than your default state would be displayed.

    Check this out! If you get any problem while doing above code, let me know.