I have a ListView
which displays news items. They contain an image, a title and some text. The image is loaded in a separate thread (with a queue and all) and when the image is downloaded, I now call notifyDataSetChanged()
on the list adapter to update the image. This works, but getView()
is getting called too frequently, since notifyDataSetChanged()
calls getView()
for all visible items. I want to update just the single item in the list. How would I do this?
Problems I have with my current approach are:
- Scrolling is slow
- I have a fade-in animation on the image which happens every time a single new image in the list is loaded.
For RecycleView please use this code
get the model class first as global like this model class object
and initialise it to global
get the current row of the view by the model by initialising the it to global model
set the changed value to global model object method to be set and add the notifyDataSetChanged its works for me
Here is a related question on this with good answers.
I used the code that provided Erik, works great, but i have a complex custom adapter for my listview and i was confronted with twice implementation of the code that updates the UI. I've tried to get the new view from my adapters getView method(the arraylist that holds the listview data has allready been updated/changed):
It's working well, but i dont know if this is a good practice. So i don't need to implement the code that updates the list item two times.
exactly I used this
The answers are clear and correct, I'll add an idea for
CursorAdapter
case here.If youre subclassing
CursorAdapter
(orResourceCursorAdapter
, orSimpleCursorAdapter
), then you get to either implementViewBinder
or overridebindView()
andnewView()
methods, these don't receive current list item index in arguments. Therefore, when some data arrives and you want to update relevant visible list items, how do you know their indices?My workaround was to:
newView()
notifyDatasetChanged()
and refreshing all of themDue to view recycling the number of view references I'll need to store and iterate will be roughly equal the number of list items visible on screen.
This is how I did it:
Your items (rows) must have unique ids so you can update them later. Set the tag of every view when the list is getting the view from adapter. (You can also use key tag if the default tag is used somewhere else)
For the update check every element of list, if a view with given id is there it's visible so we perform the update.
It's actually easier than other methods and better when you dealing with ids not positions! Also you must call update for items which get visible.