Reputation: 1238
I have a RecyclerView
with a custom Adapter
to display rectangular elements (about 15, the number can change if the user adds/removes items) in a grid (using Gridlayout Manager
).
These elements consist of an ImageView
and a TextView
. I want to update both Views regularly after receiving new results from network requests (say every 2minutes).
What would be the best way to update these views? How can I identify the specific ViewHolder
? How can I access the ViewHolder
values of these elements? Would it be best to keep a reference to each ViewHolder
in the Activity
where I want to change them? Or should I create a new element with the new values and replace the current element with it? What would be considered best practice?
Upvotes: 2
Views: 1227
Reputation: 21
The best practice is update views and it's data when some data change. You have to do this always. The method
adapter.notifyDataSetChanged
do this for you.
When you update some data you have to tell to the system, because there's an important action named recycle, when you use a view holder you giving the possibility to the system recycle a view, what means views that don't change the date are stored on the view holder, so the list doesn't need to use the
findViewById
again.
Upvotes: 1
Reputation: 2223
If you are storing your value of image view and text view in an arraylist you can call notifyDataSetChanged on the adapter when you have a change in the dataset.
For example if you have say a Grid object whose attributes include image uri(to set the image) and string(to set in text view) and some Array list say
List<Grid> gridList = new ArrayList<>();
you can update whatever new data you get in the Arraylist and after you are done updating in the array list call
adapter.notifyDataSetChanged();
Upvotes: 3