I have some problems understanding RecyclerView
s SortedList
.
Lets say I have a very simple class only having a very simple class holding data:
public class Pojo {
public final int id;
public final char aChar;
public Pojo(int id, char aChar) {
this.id = id;
this.aChar = aChar;
}
@Override
public String toString() {
return "Pojo[" + "id=" + id
+ ",aChar=" + aChar
+ "]";
}
}
My understanding is that the sorted list won't contain any duplicates.
But when I have a SortedList with callbacks like this:
....
@Override
public boolean areContentsTheSame(Pojo oldItem, Pojo newItem) {
return oldItem.aChar == newItem.aChar;
}
@Override
public int compare(Pojo o1, Pojo o2) {
return Character.compare(o1.aChar, o2.aChar);
}
@Override
public boolean areItemsTheSame(Pojo item1, Pojo item2) {
return item1.id == item2.id;
}
I end up with duplicates when I add multiple items with the same id but different chars.
sortedList.add(new Pojo(1, 'a'));
sortedList.add(new Pojo(1, 'b'));
I would expect the list to update the item. Instead now I have multiple items even though areItemsTheSame
returned true
.