Florian Mac Langlade
Florian Mac Langlade

Reputation: 1903

Android unload listview images

Please excuse my english, I'm french !

So, I have got a little question... Do you know a clean way to unload images in a listview ? I want to display images for visible items and, to free memory, unload images on the other items...

May be in a OnScrollListener ?

Thanks !

Upvotes: 3

Views: 464

Answers (2)

Piotr Chojnacki
Piotr Chojnacki

Reputation: 6867

Actually ListView is designed in that way, that user doesn't have to bother with loading and unloading it's data.

You are providing an Adapter which contains all the data (items) and you add this Adapter to your ListView which in turn knows how to manage those items - basically it loads only those items from adapter that are visible for phone's user.

There are few built-in adapters, like for example SimpleCursorAdapter, which most of the time are enough for simple needs. But if you want more extended usability, you'd rather like to write your own adapter, which in fact isn't that hard. In this tutorial, you can read about writing own adapter: http://www.ezzylearning.com/tutorial.aspx?tid=1763429

Also you can read more about ListView in Android documentation: http://developer.android.com/guide/topics/ui/layout/listview.html

Upvotes: 2

yahya
yahya

Reputation: 4860

ListView use your items as converted. So you got only 2 or 3 items more than you actually see. You don't have to unload those images. Actually better not to, because that's gonna slow your app down.

The important part is:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) ctx
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.your_item, null);
        }

  // set images on view
}

Upvotes: 1

Related Questions