How to update/refresh specific item in RecyclerView

AndroidAndroid AdapterAndroid Recyclerview

Android Problem Overview


I'm trying to refresh specific item in RecyclerView.

Story: Whenever user clicks on item, it shows AlertDialog. User can type some text by clicking ok button. I want to show this text in this item and show invisible ImageView - declared in XML and adapter ViewHolder -

I used this function in AlertDialog Positive Button to update the item:

private void updateListItem(int position) {
  View view = layoutManager.findViewByPosition(position);
  ImageView medicineSelected = (ImageView) view.findViewById(R.id.medicine_selected);
  medicineSelected.setVisibility(View.VISIBLE);
  TextView orderQuantity = (TextView) view.findViewById(R.id.order_quantity);
  orderQuantity.setVisibility(View.VISIBLE);
  orderQuantity.setText(quantity + " packet added!");

  medicinesArrayAdapter.notifyItemChanged(position);
}

But this code not only changes the itemView at passed position, but also changes some of other itemView(s) as well!

How should I change specific itemView correctly by clicking on it?

Android Solutions


Solution 1 - Android

You can use the notifyItemChanged(int position) method from the RecyclerView.Adapter class. From the documentation:

> Notify any registered observers that the item at position has changed. > Equivalent to calling notifyItemChanged(position, null);. > > This is an item change event, not a structural change event. It > indicates that any reflection of the data at position is out of date > and should be updated. The item at position retains the same identity.

As you already have the position, it should work for you.

Solution 2 - Android

#Update single item

  1. Update the data item
  2. Notify the adapter of the change with notifyItemChanged(updateIndex)

###Example

Change the "Sheep" item so that it says "I like sheep."

https://i.stack.imgur.com/UUzHr.gif" width="200" alt="Update single item">

String newValue = "I like sheep.";
int updateIndex = 3;
data.set(updateIndex, newValue);
adapter.notifyItemChanged(updateIndex);

My full answer with more examples is here.

Solution 3 - Android

Add the changed text to your model data list

mdata.get(position).setSuborderStatusId("5");
mdata.get(position).setSuborderStatus("cancelled");
notifyItemChanged(position);

Solution 4 - Android

I think I have an Idea on how to deal with this. Updating is the same as deleting and replacing at the exact position. So I first remove the item from that position using the code below:

public void removeItem(int position){
    mData.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, mData.size());
}

and then I would add the item at that particular position as shown below:

public void addItem(int position, Landscape landscape){
    mData.add(position, landscape);
    notifyItemInserted(position);
    notifyItemRangeChanged(position, mData.size());
}

I'm trying to implement this now. I would give you a feedback when I'm through!

Solution 5 - Android

I got to solve this issue by catching the position of the item that needed to be modified and then in the adapter call

public void refreshBlockOverlay(int position) {
	notifyItemChanged(position);
}

, this will call onBindViewHolder(ViewHolder holder, int position) for this specific item at this specific position.

Solution 6 - Android

A way that has worked for me personally, is using the recyclerview's adapter methods to deal with changes in it's items.

It would go in a way similar to this, create a method in your custom recycler's view somewhat like this:

public void modifyItem(final int position, final Model model) {
    mainModel.set(position, model);
    notifyItemChanged(position);
}

Solution 7 - Android

Below solution worked for me:

On a RecyclerView item, user will click a button but another view like TextView will update without directly notifying adapter:

I found a good solution for this without using notifyDataSetChanged() method, this method reloads all data of recyclerView so if you have image or video inside item then they will reload and user experience will not good:

Here is an example of click on a ImageView like icon and only update a single TextView (Possible to update more view in same way of same item) to show like count update after adding +1:

// View holder class inside adapter class
public class MyViewHolder extends RecyclerView.ViewHolder{

	ImageView imageViewLike;

	public MyViewHolder(View itemView) {
         super(itemView);

		imageViewLike = itemView.findViewById(R.id.imageViewLike);
		imageViewLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = getAdapterPosition(); // Get clicked item position
                TextView tv = v.getRootView().findViewById(R.id.textViewLikeCount); // Find textView of recyclerView item
                resultList.get(pos).setLike(resultList.get(pos).getLike() + 1); // Need to change data list to show updated data again after scrolling
                tv.setText(String.valueOf(resultList.get(pos).getLike())); // Set data to TextView from updated list
            }
        });
    }

Solution 8 - Android

I tried this and it worked for me:

private void updateItem(Object item, int position, int itemValue){
   item.setItemNumber(itemValue) // update item field
   notifyItemChanged(position, item);
}

Make sure to pass in the object of the item you want to update or refresh, its position in the list and value.

Solution 9 - Android

The problem is RecyclerView.Adatper does not provide any methods that return the index of element

public abstract static class Adapter<VH extends ViewHolder> {
  /**
   * returns index of the given element in adapter, return -1 if not exist
   */
  public int indexOf(Object elem);
}

My workaround is to create a map instance for (element, position)s

public class FriendAdapter extends RecyclerView.Adapter<MyViewHolder> {
  private Map<Friend, Integer> posMap ;
  private List<Friend> friends;

  public FriendAdapter(List<Friend> friends ) {
    this.friends = new ArrayList<>(friends);
    this.posMap = new HashMap<>();
    for(int i = 0; i < this.friends.size(); i++) {
      posMap.put(this.friends.get(i), i);
    }
  }

  public int indexOf(Friend friend) {
    Integer position = this.posMap.get(elem);
    return position == null ? -1 : position;
  }
  // skip other methods in class Adapter
}
  • the element type(here class Friend) should implements hashCode() and equals() because it is key in hashmap.

when an element changed,

  void someMethod() {
    Friend friend = ...;
    friend.setPhoneNumber('xxxxx');

    int position = friendAdapter.indexOf(friend);
    friendAdapter.notifyItemChanged(position);

  }

It is good to define an helper method

public class FriendAdapter extends extends RecyclerView.Adapter<MyViewHolder> {

  public void friendUpdated(Friend friend) {
    int position = this.indexOf(friend);
    this.notifyItemChanged(position);
  }
}

Map instance(Map<Friend, Integer> posMap) is not necessarily required. If map is not used, looping throughout list can find the position of an element.

Solution 10 - Android

notifYItemsChanged will inform that some item is changed and hence the all list will be updated regardless of which item was changed. Instead you should use DiffUtil.

Read about them here: https://developer.android.com/reference/androidx/recyclerview/widget/DiffUtil

Solution 11 - Android

That's also my last problem. Here my solution I use data Model and adapter for my RecyclerView

 /*Firstly, register your new data to your model*/
 DataModel detail = new DataModel(id, name, sat, image);
 
 /*after that, use set to replace old value with the new one*/
 int index = 4;
 mData.set(index, detail);

 /*finally, refresh your adapter*/
 if(adapter!=null)
    adapter.notifyItemChanged(index);

Solution 12 - Android

you just have to add following code in alert dialog box on save click

          recyclerData.add(position, updateText.getText().toString());
          recyclerAdapter.notifyItemChanged(position);

Solution 13 - Android

if you are creating one object and adding it to the list that you use in your adapter , when you change one element of your list in the adapter all of your other items change too in other words its all about references and your list doesn't hold separate copies of that single object.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElgendyView Question on Stackoverflow
Solution 1 - AndroidEdson MenegattiView Answer on Stackoverflow
Solution 2 - AndroidSuragchView Answer on Stackoverflow
Solution 3 - AndroidShamir KpView Answer on Stackoverflow
Solution 4 - AndroidJoseph JoestarView Answer on Stackoverflow
Solution 5 - AndroidhaneenCoView Answer on Stackoverflow
Solution 6 - Androiduser3709410View Answer on Stackoverflow
Solution 7 - AndroidTouhidView Answer on Stackoverflow
Solution 8 - Androidali sampsonView Answer on Stackoverflow
Solution 9 - Androidchmin.seoView Answer on Stackoverflow
Solution 10 - AndroidGaurView Answer on Stackoverflow
Solution 11 - Androiddeya triView Answer on Stackoverflow
Solution 12 - AndroidDhruvishaView Answer on Stackoverflow
Solution 13 - AndroidA.sobhdelView Answer on Stackoverflow