Home > Back-end >  How do I make Recycler view replace existing items instead of adding to it?
How do I make Recycler view replace existing items instead of adding to it?

Time:12-18

I've figured out how to get the sorted data with a query and pass that to the Recycler view, but it only adds to the RecyclerView instead of replacing the already existing items. Uploaded the code to Pastebin since code irrelevant to the problem is in the class.

MyDatabaseHelper class = https://pastebin.com/fXHw5ccb

CustomAdapter class = https://pastebin.com/4epLqWzJ

MainActivity class = https://pastebin.com/h5mDQunv

Including

    customAdapter.notifyDataSetChanged();
    recyclerView.setAdapter(customAdapter);

or removing it doesn't solve the problem.

How it looks: https://i.imgur.com/C60ur8b.png

How I want it to look after clicking the menu sort item: https://i.imgur.com/wjZP9yX.png

CodePudding user response:

It looks like you call storeFoodDataInArrayList() multiple times which populates the food_ID array, but you never clear that array.

Clear the food_ID array before populating it.

CodePudding user response:

Solved: Had to pass Custom Adapter to a custom object and read the ArrayList from the object, which let me sort based on SQL queries.

  • Related