Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android add/replace Items within RecyclerView

I know there are lots of threads already on this topic, but none of the given solutions worked for me so far. I'm trying to add or update an item of a RecyclerView. Here's my code so far:

MainActivity

private MyListItemAdapter mAdapter;
private RecyclerView recyclerView;

// called on activity create
private void init() {
    // initialize activity, load items, etc ...
    mAdapter = new MyListItemAdapter(this, items);
    recyclerView.setAdapter(mAdapter);
}

// called when I want to replace an item
private void updateItem(final Item newItem, final int pos) {
    mAdapter.replaceItem(newItem, pos);
}

MyListItemAdapter

public class MyListItemAdapter extends RecyclerView.Adapter<MyListItemAdapter.MyListItemViewHolder> {

    private List<Item> mItems;

    public void replaceItem(final Item newItem, final int pos) {
        mItems.remove(position);
        mItems.add(position, newItem);

        notifyItemChanged(position);
        notifyDataSetChanged();
    }    
}

I tried to make this changes from the MainActivity aswell, but in every case I tried my list doesn't get updated. The only way it worked was when I reset the adapter to the recyclerView:

mAdapter.notifyDataSetChanged();
recyclerView.setAdapter(mAdapter);

which obviously is a bad idea. (aside from the bad side effects wouldn't even work when I'm using lazy loading on my lists).

So my question is, how can I make notifyDataSetChanged() work properly?

edit

I found a solution for replacing items. After mAdapter.replaceItem(newItem, pos); I had to call recyclerView.removeViewAt(position);

This works for replacing an item, but doesn't solve my problem when I want to add items (e.g. lazy loading) to my list

edit2

I found a working solution for adding items

Adapter:

public void addItem(final Item newItem) {
    mItems.add(newItem);
    notifyDataSetChanged();
}

Activity:

private void addItem(final Item newItem) {
    mAdapter.addItem(newItem);
    recyclerView.removeViewAt(0); // without this line nothing happens
}

For some reason this works (also: it doesn't remove the view at position 0), but I'm sure this isn't the correct way to add items to a recyclerView

like image 351
Markus Avatar asked Mar 14 '23 07:03

Markus


2 Answers

This should work:

private ArrayList<Item> mItems;

public void replaceItem(final Item newItem, final int position) {
    mItems.set(position, newItem);
    notifyItemChanged(position);
}  

ArrayList.set() is the way to go to replace items.

For adding items, just append them to mItems and then go notifyDatasetChanged(). Another way to go is to use notifyItemRangeInserted(). Depending on where/how are you adding new items and how many of them, it might be worth it.

like image 50
natario Avatar answered Mar 16 '23 20:03

natario


Use

 mItems.set(position, newItem);

instead of

 mItems.add(position, newItem);

because .set method will replace your data to particular position.

like image 36
Piyush Avatar answered Mar 16 '23 19:03

Piyush