How to update/refresh specific item in RecyclerView
Asked Answered
H

13

149

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?

Horrid answered 8/9, 2015 at 12:5 Comment(0)
C
120

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.

Carolann answered 8/9, 2015 at 12:19 Comment(9)
I already used it but if i want to update position 0, it is updating position 0, 9, 19, ...Horrid
Sorry, did not understand your comment. Which position is it updating? 0, 9 or the range between 0 and 9?Carolann
No, it is not range, every time I am updating item, it refreshing unwanted items as wellHorrid
@Learner When you update the ViewHolder at position 0, it stays updated. The RecyclerView will recycle that ViewHolder and use it for position 9, so you're see those changes there. I recommend setting this text in onBindViewHolder() instead - that way, whenever a ViewHolder is recycled you can check its position and set text or make image visible or whatever correctly.Lagena
@Lagena still same problem doing changes in onBindViewHolder()Tamaratamarack
@Tamaratamarack you should probably make a new question; what does your onBindViewHolder() look like?Lagena
If you're changing drawables you'll need to mutate() the drawable, otherwise all drawables will be updated.Sunless
@EdsonMenegatti want to talk here is my email: [email protected]Eakins
@Elgendy it has been some time already, but did you solve the issue with the 0, 9 positions updating?Kinder
I
71

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."

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.

Igal answered 24/2, 2018 at 3:59 Comment(4)
how do to that if I wanna the updated data switch to the topStellite
@Choy, after you update the data you can move it to index 0. See the Move Single Item section of this itemIgal
@Igal may I know which callback does it call to update the item in adapter. It certainly does not call onBindViewHolder method, I logged it.Sorn
@SouravKannanthaB, Sorry, I'm not sure. I switched to Flutter so I've forgotten a lot of things about Android. (ListViews, by the way, are much, much easier to deal with in Flutter. I'm very happy not to be working with RecyclerViews anymore.)Igal
C
17

Add the changed text to your model data list

mdata.get(position).setSuborderStatusId("5");
mdata.get(position).setSuborderStatus("cancelled");
notifyItemChanged(position);
Claycomb answered 5/2, 2019 at 5:21 Comment(1)
see here how to get position #53707330Blabber
E
13

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!

Eyed answered 19/4, 2016 at 15:7 Comment(2)
You are doing one extra step. Just replace the element at position and call item notifyItemChanged(position);Expulsion
@toshkinl How would you know when data is coming from JSONObject? with no position?Encampment
C
9

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.

Carvajal answered 10/5, 2016 at 8:22 Comment(0)
I
3

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);
}
Impedance answered 24/3, 2018 at 9:32 Comment(2)
this is correct answer, but you should explain it how to change in views.Aswan
how to do you get position from recycler view say based on a unique user idBlabber
P
2

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
            }
        });
    }
Paraphrase answered 2/8, 2018 at 6:9 Comment(0)
I
2

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.

Illustrate answered 10/8, 2021 at 18:19 Comment(0)
A
0

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.

Ammonite answered 19/11, 2019 at 2:33 Comment(0)
B
0

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

Bowker answered 22/11, 2021 at 5:22 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. link-only-answersMelchor
A
-1

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);
Acedia answered 25/10, 2017 at 6:50 Comment(0)
F
-1

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

          recyclerData.add(position, updateText.getText().toString());
          recyclerAdapter.notifyItemChanged(position);
Flagpole answered 20/10, 2018 at 7:47 Comment(0)
M
-1

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.

Mohawk answered 17/4, 2019 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.