khaleel_jageer
khaleel_jageer

Reputation: 1502

How to update single item in recyclerview?

I'm trying to update the recyclerview item using following code snippet.

public void setItemToPostion(WatchListEpisodeBean watchListBean, int itemPosition) {
    this.watchListBean.add(itemPosition, watchListBean);
    notifyItemChanged(itemPosition);
}

It works fine. That's item at position updated with new data. But the problem is item cloned to its next position(appears in next position). I don't find what's wrong with my code.

This is appcompat & support design lib versions:

compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'

Please help me to fix this. Thanks in advance.

Upvotes: 1

Views: 8992

Answers (3)

Gaurav Singh
Gaurav Singh

Reputation: 2320

If you are simply updating one part of the view, use the notifyItemChanged() instead of notifiyDataSetChanged(). The difference here has to do with structural changes vs item changes. This is on the android developers RecyclerView.Adapter documentation found here.

Here is another tidbit on the differences between the two types of changes:

There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred. Structural changes are when items are inserted, removed or moved within the data set.

Upvotes: 2

AJay
AJay

Reputation: 1193

Your item is clone because you are adding item.

Try to use set instead of add like this

public void setItemToPostion(WatchListEpisodeBean watchListBean, int itemPosition) {
    this.watchListBean.set(itemPosition, watchListBean);
    notifyItemChanged(itemPosition);
}

Upvotes: 7

Jinal Patel
Jinal Patel

Reputation: 709

Use notifyDataSetChanged(); instead of notifyItemChanged(itemPosition);

Upvotes: -4

Related Questions