Control which items to redraw? (notifyDataSetChanged with a range)

1439 views
Skip to first unread message

twan

unread,
May 28, 2009, 9:51:21 AM5/28/09
to Android Developers
Goodmorning,

A call to notifyDataSetChanged() on a ArrayAdapter causes all items in
a listview to redraw. This is quite ineffecient.

At swing you can control which items (or which range of items) redraw.
Is this also possible with android?

Kind regards,
TWan

Streets Of Boston

unread,
May 28, 2009, 10:35:49 AM5/28/09
to Android Developers
If it is just a refresh and not an entirely new list or additions/
removals of elements, use notifyDataSetInvalidated() instead. I think
it's a bit more efficient.

But, as far as i know, you can't specifiy a range of elements. But you
can optimize it yourself.
I do this at times using the list-item view's getTag() and setTag()
methods. If the getTag() returns an object that is different than you
would expect (with respect to the list-item's position), refresh the
view's contents. Otherwise, leave it alone.

Romain Guy

unread,
May 28, 2009, 12:17:45 PM5/28/09
to android-d...@googlegroups.com
No, notifyDataSetInvalidated() causes the entire content of ListView
to disappear. It's even more inefficient than notifyDataSetChanged().

If you want to refresh the content of a specific item, call
invalidate() on its View. If you changed the content of the Adapter
though, call notifyDataSetChanged().


--
Romain Guy
Android framework engineer
roma...@android.com

Note: please don't send private questions to me, as I don't have time
to provide private support. All such questions should be posted on
public forums, where I and others can see and answer them

Streets Of Boston

unread,
May 28, 2009, 12:27:37 PM5/28/09
to Android Developers
Aha! I stand corrected.
That is not what i understood from the API documentation...

Romain,
could you describe the exact differences (and usage example) of the
two?

Thanks!

About calling 'invalidate()':
Does calling invalidate() on a list-item's View cause the 'getView()'
method of an Adpater to be called?
If i'm not mistaken, getView() does not get called on 'invalidate()'.
But i could be wrong. TWan may need the adapter's getView() to be
called for specific items to properly update the items' views.

On May 28, 12:17 pm, Romain Guy <romain...@google.com> wrote:
> No, notifyDataSetInvalidated() causes the entire content of ListView
> to disappear. It's even more inefficient than notifyDataSetChanged().
>
> If you want to refresh the content of a specific item, call
> invalidate() on its View. If you changed the content of the Adapter
> though, call notifyDataSetChanged().
>
> --
> Romain Guy
> Android framework engineer
> romain...@android.com

Romain Guy

unread,
May 28, 2009, 12:31:23 PM5/28/09
to android-d...@googlegroups.com
notifyDataSetInvalidated() is used when the adapter loses all of its
content. It's there to tell ListView to stop using the adapter
basically. This is usually invoked when you have a Cursor and you call
close() on the Cursor.
roma...@android.com

Streets Of Boston

unread,
May 28, 2009, 12:36:32 PM5/28/09
to Android Developers
Thanks!
The fact that it's used to 'tell the list view to stop using the
adapter' might actually explain an issue i have with a list of images
i show in my own app. :)
> public forums, where I and others can see and answer them- Hide quoted text -
>
> - Show quoted text -

twan

unread,
May 28, 2009, 2:58:32 PM5/28/09
to Android Developers
Thanks all for the help! I'm going to give invalidate a try ;)

On May 28, 6:17 pm, Romain Guy <romain...@google.com> wrote:
> No, notifyDataSetInvalidated() causes the entire content of ListView
> to disappear. It's even more inefficient than notifyDataSetChanged().
>
> If you want to refresh the content of a specific item, call
> invalidate() on its View. If you changed the content of the Adapter
> though, call notifyDataSetChanged().
>
> --
> Romain Guy
> Android framework engineer
> romain...@android.com
Reply all
Reply to author
Forward
0 new messages