DiffUtil Callback on different size of list
Asked Answered
D

1

7

I am comparing the two list to find the differences between them using a custom implementation DiffUtil Callback but those two list have different size and when i logged the elements being compared in both the list in 'areItemsTheSame' and 'areContentsTheSame' elements that are compared are limited by the size of the list with least elements and hence when in my adapter i call 'dispatchUpdatesTo' the new item is added but its content are same as the last items from old list

Implementation of those methods :

@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
    return mOldList.get(oldItemPosition).equals(mNewList.get(newItemPosition));
}

@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
    return mOldList.get(oldItemPosition).equals(mNewList.get(newItemPosition));
}

Eg :

(Old)List A = "A","B","C","D","E","F","G","I"

(New)List B = "A","B","C","D","E","F","G","I","K"

upon logging i found that 'areItemsTheSame' and 'areContentsTheSame' only compares element up-to "I" and hence returns true for all calls and after calling dispatchUpdatesTo for adapter new item is added to list but it is "I" not "k"

So what i get is List A = "A","B","C","D","E","F","G","I","I"

Dunne answered 8/7, 2017 at 6:31 Comment(3)
have you found out a solution?Sarah
Is your item "K" visible on the screen?Douceur
Any solution yet?Hayfork
G
1

If your adapter is set with setHasStableIds(true), position is unreliable and you need to override the getItemId() method.

Greenburg answered 28/12, 2023 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.