Hello everyone! I'm trying to use a ListAdapter, ...
# android
f
Hello everyone! I'm trying to use a ListAdapter, with a list in which items are not unique. Items are kinda like this:
data class Item(val a: String, val b: String)
My problems comes trying to create a DiffCallback: If I use oldItem == newItem on
itemsAreTheSame
, this is going to return true on items that are not the same, for instance, the first two on the following list: Item("a", "a") Item("a", "a") Item("b", "b") As long as an operation doesn't involve items with the same content, everything works fine: animations, add, edit, move, delete. But this gets buggy when items are equal. If I delete the second item, the first one will also be deleted. If I add or move something, the animation is wrong, kind of reversed. What I wanted to know is if there's any other way to handle something like this, without adding an identifier, nor using a custom differ without ListAdapter.
u
You need to add one more key in your data class Say id:Int that must have unique value for each item. As long as the data is same so the hashcode will also get generated same so you can't use it
f
Thanks! I actually solved it like that, but I was wondering if there was another way, so I wouldn't add an id just for this 😅