RecycylerView with SortedList shows duplicate items

Multi tool use
RecycylerView with SortedList shows duplicate items
I use an SortedList
with a RecyclerViewAdapter
for my list. Therefore I send my items (loaded from my backend in a background thread) via an listener to my GameListAdapter
. According to the implemented logic in my SortedListAdapterCallback
items sent to the adapter with the same id should be replaced instead of being inserted multiple times. Unfortunately this is mainly, but not always the case.
SortedList
RecyclerViewAdapter
GameListAdapter
SortedListAdapterCallback
This is the constructor of my GameListAdapter
GameListAdapter
public GameListAdapter(RecyclerView list) {
this.list = list;
gamelistItems = new SortedList<>(GameListItem.class, new SortedListAdapterCallback<GameListItem>(this) {
@Override
public int compare(GameListItem o1, GameListItem o2) {
return o1.getDate().compareTo(o2.getDate());
}
@Override
public boolean areContentsTheSame(GameListItem oldItem, GameListItem newItem) {
if (oldItem instanceof GameListHeader && newItem instanceof GameListHeader) {
return oldItem.getDate().equals(newItem.getDate());
} else if (oldItem instanceof Game && newItem instanceof Game) {
Game gameOld = (Game) oldItem;
Game gameNew = (Game) newItem;
if (gameOld.getId() != gameNew.getId()) {
return false;
}
if (!gameOld.getTeamHome().equals(gameNew.getTeamHome())) {
return false;
}
if (!gameOld.getTeamAway().equals(gameNew.getTeamAway())) {
return false;
}
if (gameOld.getScoreHome() != gameNew.getScoreHome()) {
return false;
}
if (gameOld.getScoreAway() != gameNew.getScoreAway()) {
return false;
}
if (!gameOld.getState().equals(gameNew.getState())) {
return false;
}
return true;
}
return false;
}
@Override
public boolean areItemsTheSame(GameListItem item1, GameListItem item2) {
if (item1 instanceof GameListHeader && item2 instanceof GameListHeader) {
return item1.getDate() == item2.getDate();
} else if (item1 instanceof Game && item2 instanceof Game) {
return ((Game) item1).getId() == ((Game) item2).getId();
}
return false;
}
});
DataStorage.getInstance().registerListener(this);
}
The items are added to the list in this method:
@Override
public void onAddedGame(Game game) {
Handler handler = new Handler();
handler.post(() -> gamelistItems.add(game));
}
Even though the items have the same id, I see this in my list:
https://i.stack.imgur.com/vWYml.png
1 Answer
1
You compare the response from GameListHeader.getDate()
two different ways. In areContentsTheSame()
it's
GameListHeader.getDate()
areContentsTheSame()
return oldItem.getDate().equals(newItem.getDate());
and in areItemsTheSame()
you use
areItemsTheSame()
return item1.getDate() == item2.getDate();
Although it is not clear from your posting how the response from getDate()
is generated or what the data type is, I do think that one of these is incorrect. If getDate()
returns a String
then String.equals(String)
is what you want and ==
is incorrect.
getDate()
getDate()
String
String.equals(String)
==
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.