-1

I have a Model UniqueUserTransModel and a UniqueArrayList. Initially, I have the List and I am passing it to the Adapter.

Old List ->

pos : 0, name : "user 1", transcount : 10
pos : 1, name : "user 2", transcount : 9
pos : 2, name : "user 3", transcount : 8
pos : 3, name : "user 4", transcount : 7
pos : 4, name : "user 5", transcount : 6

After Updating I have a NewList and I set it to UniqueArrayList = (newly Computed ArrayList).

New List ->

pos : 0, name : "user 2", transcount : 11
pos : 1, name : "user 1", transcount : 9
pos : 2, name : "user 3", transcount : 8
pos : 3, name : "user 4", transcount : 7
pos : 4, name : "user 5", transcount : 6

And I just called notifydatasetChanged from the Fragment.

The first two items get swapped in the NewList but are not reflecting in the recyclerview.

Why it is not reflecting? I know if I clear the old list and freshly add the new one it will work or if i add or remove a new Item then also it will work.

Aman Verma
  • 3,155
  • 7
  • 30
  • 60
  • can you provide your adapter and fragment code??? – hamid_c Feb 23 '20 at 13:18
  • It's complicated. That's y I put it like this. what do you want to know? – Aman Verma Feb 23 '20 at 13:26
  • i want to know how you update your adapter's list – hamid_c Feb 23 '20 at 13:32
  • I use Realm and I need to do some computation so I take a Hashmap and do some computation like count the number of trans per user. So I increase the count and make a Hashmap and put all the values into an ArrayList. And now when a new trans I being made, the whole code re-run and I will get a newly updated list from the HashMap sorted by the no. of trans. Without clearing, I was just passing the ArrayList = (newly generated list from hmap), but the adapter was not changing. – Aman Verma Feb 23 '20 at 13:45
  • it seems to be ok as you describe the process, but really not helpful to tell what's wrong in your code – hamid_c Feb 23 '20 at 13:48

1 Answers1

0

Do not create a new Arraylist for Adapter.

Clear the arraylist and add all the items into it from new arraylist.

Old ArrayList

UniqueArrayList.clear()

New Arraylist

UniqueArrayList.addAll(**name of new arraylist**)

Now again reload the dataset.

notifyDataSetChanged()
Prashant Kumar Sharma
  • 1,120
  • 1
  • 11
  • 21