views:

34

answers:

1

Let's assume i got a list of ImageView and TextView with LinearLayout. Now i'm using simple ArrayAdapter extension that gets Strig[][] as items which is an array of double strings (each entry contains String[2] where the first string is a uri to the image and the second one is the text. i override getView to display the image and text , recyling views, of course :-). However since image loading, even from resources may be slow, i'm doing it in an async task, that means that i have a default image loaded and i use: startLoading (it's my method) that stakes the ImageView and the Uri and once the image is ready it places it in the image view.

However sometimes, since it's a list if the image is not loaded yet and I'm in the middle of the process it's worth while stopping the loading, and that's in case that the line i'm loading image for is no longer visible.

Since the views are recycled i can't check if ImageView is visible since it may be visible but with a different Drawable... is there a way for me to know at some point that my line in invalidated and not visible ? is there a simple way or shell i need to start using flags and tag pattern and stuff like that to discover that my line is no longer valid ?

A: 

What I do is based on how the ListView manages it's views.

As there is a limited set of Views really used by the ListView and that might be passed to our Adapter through the convertView argument of the getView() method, I implemented the following:

  • I have a LinkedList class member where I store the Uris to be loaded. These Uris are consumed in an AsyncTask member to retrieve the pics.
  • Before returning the item view in getView(), my Adapter places the Urin of the item pic in the View.setTag(Object).
  • When I reuse a convertView, before reusing it's content I get the Uri it was previously linked to using View.getTag() and remove the corresponding element from the pending Uris LinkedList.
Kevin Gaudin
codeScriber
I just described the system to avoid loading pictures which are not needed anymore since the user scrolled further. The AsyncTask tells the Adapter to update the item ImageView when the image is loaded, be it a reused view or not. The Adapter places the pic if it's already loaded or a default pic if it's not.
Kevin Gaudin
this is pretty close to what i'm doing, so let me just be sure:1.you put a default image2. you start an async task for loading the image - saving an instance of the ImageView3. once the task is complete you put the Drawable in the tag and calls the adapter's dataChanged ?
codeScriber