How to update / update a specific item in RecyclerView

I am trying to update a specific item in a RecyclerView .

History: When the user clicks on an element, it shows AlertDialog . The user can enter the text by clicking the "OK" button. I want to show this text in this element and show an invisible ImageView - declared in XML and ViewHolder adapter -

I used this function in AlertDialog Positive Button to update an element:

 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 in the passed position, but also changes some other itemView elements!

How can I correctly modify a specific ItemView by clicking on it?

+80
android android-recyclerview recycler-adapter android-adapter
Sep 08 '15 at 12:05
source share
12 answers

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

Notify registered observers that the position in the position has changed. Equivalent to calling notifyItemChanged (position, null) ;.

This is a position change event, not a structural change event. This indicates that any reflection of the data in the position is outdated and should be updated. The position in the position retains the same identity.

Since you already have a position, it should work for you.

+81
Sep 08 '15 at 12:19
source share

Update single item

  1. Update item
  2. Notify adapter of change using notifyItemChanged(updateIndex)

example

Change β€œSheep” to say β€œI love sheep.”

Update single item

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

My complete answer with lots of examples here .

+23
Feb 24 '18 at 3:59
source share

I think I have an idea on how to deal with this. Updating is the same as removing and replacing in the exact position. Therefore, I first remove the element from this position using the following code:

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

and then I would add the element in this particular position, as shown below:

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

I am trying to implement this now. I will give you a review when I pass!

+6
Apr 19 '16 at 15:07
source share

I decided to solve this problem by catching the position of the element that needs to be changed and then in the adapter call

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

this will call a BindViewHolder (ViewHolder holder, int position) for this particular item at that particular position.

+6
May 10 '16 at 8:22
source share

Add the modified text to your model data list.

 mdata.get(position).setSuborderStatusId("5"); mdata.get(position).setSuborderStatus("cancelled"); notifyItemChanged(position); 
+3
Feb 05 '19 at 5:21
source share

In your adapter class, in the onBindViewHolder method, set the ViewHolder to setIsRecyclable (false), as shown below.

 @Override public void onBindViewHolder(RecyclerViewAdapter.ViewHolder p1, int p2) { // TODO: Implement this method p1.setIsRecyclable(false); // Then your other codes } 
+1
Dec 23 '17 at 17:56 on
source share

The way that worked for me personally is to use the utility adapter methods to view the changes in these elements.

This will happen something like this: create a method in your custom recirculator view something like this:

 public void modifyItem(final int position, final Model model) { mainModel.set(position, model); notifyItemChanged(position); } 
+1
Mar 24 '18 at 9:32
source share

Below solution worked for me:

In the RecyclerView element, the user clicks a button, but another view, such as TextView, is updated without directly notifying the adapter:

I found a good solution for this without using the notifyDataSetChanged () method, this method reloads all the recyclerView data, so if you have an image or video inside the element, it will reload and the user experience will be bad:

Here is an example of clicking on an icon similar to ImageView and updating only one TextView (you can update more views in the same way for the same element) to display the update as quantities 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 } }); } 
+1
Aug 02 '18 at 6:09
source share

you just need to add the following code in the warning dialog when you click Save

  recyclerData.add(position, updateText.getText().toString()); recyclerAdapter.notifyItemChanged(position); 
0
Oct 20 '18 at 7:47
source share

if you create one object and add it to the list that you use in your adapter, when you change one element of your list in the adapter, all the rest of your elements also change, in other words, it's all about links, and your list does not keep separate copies of this one object.

0
Apr 17 '19 at 14:42
source share

This is also my last problem. Here is my solution. I am using the 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); 
-one
Oct 25 '17 at 6:50
source share

In your RecyclerView adapter, you must have an ArrayList, as well as one addItemsToList(items) method to add list items to an ArrayList. Then you can add list items by calling adapter.addItemsToList(items) dynamically. After all list items are added to the ArrayList, you can call adapter.notifyDataSetChanged() to display the list.

You can use notifyDataSetChanged in the adapter for RecyclerView

-5
Sep 08 '15 at 12:50
source share



All Articles