0

When a list is shown and scrolled around, ListAdapter.getView() either creates a new list item view, or retools an existing view passed to it (as "convertView) to display new content.

I'm wondering, if there's a way to get all the views created by getView()? E.g. if the list item views needs to free some resource when the activity is destroyed, it would be nice to go through all list item views and tell them to release the resource.

Charles
  • 50,943
  • 13
  • 104
  • 142
SVD
  • 4,743
  • 2
  • 26
  • 38

4 Answers4

4

Well, it's not as simple as iterating through the ListView using ListView#getChildAt(int) - the ListView is an AbsListView subclass - and AbsListView implements the RecycleBin used to hold Views that will be reused.

Before a View is placed in the RecycleBin its detached from its parent (the list view) using ViewGroup#detachViewFromParent(View), thus rendering it unaccessible using getChildAt(int).

What you should implement is the RecyclerListener interface that will notify you when a View is moved to the RecycleBin.

Jens
  • 16,853
  • 4
  • 55
  • 52
0

Since ListView is a ViewGroup, you should be able to iterate on child views like this:

for (int i=0; i < myListView.getChildCount(); i++) {
    View v = myListView.getChildAt(i);

    //Check to see if this view has the resource, then release it
}

Something similar was needed in this question: Android: Access child views from a ListView

Community
  • 1
  • 1
David Merriman
  • 6,056
  • 1
  • 18
  • 18
  • 1
    But that would give me only the views that are currently displayed, while some views may be cached but not shown. – SVD Jan 23 '12 at 20:17
  • You're right about that. Jens' answer of implementing RecyclerListener is a better answer. I was unaware such a thing existed, but it should be exactly what you need. – David Merriman Jan 23 '12 at 20:34
0

It is java. You can't order the application or instance to be really destroyed so as to free resources. With finalize() you can only rise the probability that java machine will do it.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • I'm not looking for a way to instantly destroy stuff, but for a way to release a reference to a resource when I'm sure it will be no longer needed (i.e. when the Activity's onDestroy is called). – SVD Jan 23 '12 at 20:19
  • What is the difference between two variants: 1. A could release B and B could release C. 2. B could release C. ? ---- You make the building one storey less, but you can't erase it. What you want to do is not supported in Android, according to many gurus here. – Gangnus Jan 23 '12 at 20:40
0

Unless your adapter is used outside of this Activity (in which case, you don't really know that you no longer need these views right?), your only reference(s) to this adapter is within the Activity. That means that if your Activity is garbage collected your adapter will be garbage collected, which means that your views will get collected. I wouldn't bother trying to optimize this unless you have a measurable problem with this behavior.

kabuko
  • 36,028
  • 10
  • 80
  • 93