Good day. I have a small chat app where i want to simply delete the chat item locally.
By debugging around i can reckon that there is something wrong with notifyItemRemoved() as follows :
• I have debugged the returned index from my List and the index was correct for list.
• I have debugged the returned Model Class from the ViewHolder and the returned model class was correct.
• I have debugged my custom equals()
method inside my Model and it was comparing correctly returning correct item.
The main complain about my debugging is i dont know why,but the equals()
method is being called 2 times...pretty weird though if you would have time please consider this case as well.
The main problem is the notifyItemRemoved() is always removing not the correct item,but always 1 item above the item which must be deleted (in means of deleted i mean deleted from the view as deleting from the list is happening correctly)
So i have no clue what is going on here.
Here is my custom equals()
methods
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj instanceof ChatModel) {
ChatModel chatModel = (ChatModel) obj;
if (chatModel.getMessageId().equals(messageId)) {
return true;
}
}
return false;
}
@Override
public int hashCode() {
return messageId.hashCode();
}
Here is how i insert items into adapter list.
For the list of models->
public void insertChatModelWithItemNotify(List<ChatModel> chatModel, boolean firstPaging) {
if (firstPaging) {
this.chatModelList.addAll(chatModel);
notifyDataSetChanged();
} else {
this.chatModelList.addAll(0, chatModel);
notifyItemRangeInserted(0, chatModel.size());
}
}
For a single model - >
public void insertChatModel(ChatModel chatModel) {
this.chatModelList.add(chatModel);
notifyDataSetChanged();
}
Here is how i remove the item from the list
public void removeItem(ChatModel chatModel) {
int position = this.chatModelList.indexOf(chatModel);
this.chatModelList.remove(chatModel);
notifyItemRemoved(position);
}
Any ideas?
You are probably adding some kind of header to the RecyclerView which makes the object index in your object list different from the View index in the RecyclerView.