Search code examples
androidlistviewadapterlistactivityandroid-listfragment

Android: migrate from ListActivity to ListFragment


the question has already been asked by someone but I couldn't find any complete answer. I've written a ListActivity (a list of monuments) with an ArrayAdapter. Bitmaps are loaded in background, saved in cache, in DB, in disk and then returned. The user can change the elements in the adapter with a click, so the method setListAdapter is called several times.

In the ListActivity the only thing is done, right now, is ask the server (or the DB) for a list of places, and call setListAdapter on an adapter with that places.

In the method getView of the adapter, the only thing I have is calling a method "loadBitmap(..)" that does everything in background, and populate the ImageViews of the listView with the bitmaps.

Everything's working fine, but: I want to use a ListFragment instead of a ListActivity for many reasons (first is that this list will be placed in a ViewPager, as a tab).

What do I have to do? Can I use the same adapter or I have to change something in there?

Thank you very much,

Lorenzo


Solution

  • ListFragment is very similar to ListActivity. It provides the same API like setListAdapter etc You will be able to use the same adapter with it. In case you need a Context or the enclosing Activity you can call ListAdapter.getActivity