Serhii K.
12/19/2019, 9:22 PMPagedListAdapter
I populate it using LivePagedListBuilder
. Each list item has Favorite button
. How it would be better to handle Favorite button
state? I need to know if an item is already added to favorites and when the button is pressed I need to update Favorites database
to delete or add this item. (I'm using architecture components in my app). Thank you.
My solution:
Inside onBindViewHolder
I call the callback to my activity and pass an item and its position. ViewModel checks if this item is favorited or not and returns a result back to the activity, the activity calls adapter.notifyItemChanged(position)
. Same goes for Favorite button
click listener. I need to pass an adapter position to ViewModel I don't like it. I think this solution is really bad but I can't come up with something better.Ben Abramovitch
12/19/2019, 9:42 PMSerhii K.
12/19/2019, 9:46 PMPagedListAdapter
Ben Abramovitch
12/19/2019, 9:47 PMval index = List.indexOf { it.id == clickedId }
if(index != -1) {
notifyItemUpdated(index)
}
Serhii K.
12/19/2019, 9:51 PMBen Abramovitch
12/19/2019, 9:52 PMSerhii K.
12/19/2019, 9:52 PMBen Abramovitch
12/19/2019, 9:54 PMSerhii K.
12/19/2019, 9:54 PMBen Abramovitch
12/19/2019, 9:55 PMSerhii K.
12/19/2019, 9:58 PMBen Abramovitch
12/19/2019, 10:00 PMSerhii K.
12/19/2019, 10:01 PMBen Abramovitch
12/19/2019, 10:01 PMSerhii K.
12/19/2019, 10:03 PMBen Abramovitch
12/19/2019, 10:04 PMSerhii K.
12/19/2019, 10:04 PMisFavorited
infoBen Abramovitch
12/19/2019, 10:05 PMSerhii K.
12/19/2019, 10:09 PMisFavorited
before the list is passed to the adapter. In that case I don't need to have in memory list of favoritesBen Abramovitch
12/19/2019, 10:10 PMSerhii K.
12/19/2019, 10:19 PMSELECT * FROM MyTable WHERE id IN (1, 2, 3, 4)
where 1, 2, 3, 4 are idsBen Abramovitch
12/19/2019, 10:47 PMChris Craik
12/20/2019, 2:52 AMSerhii K.
12/20/2019, 9:55 AMzhuinden
01/03/2020, 10:38 AMadapter.notifyItemChange∂
at all? You should be able to handle this just by writing to DB, and then LivePagedListBuilder
would emit a new PagedList, and PagedListAdapter would handle the diffSerhii K.
01/04/2020, 5:12 PM