I am having an activity with a listview in it. The listview is populated from a large XML pulled from a server.
My scenario usually is to have a SAX parser, parse the XML and return a Vector(or similar structure) with the parsed data.
The problem is that the xml is too big and the Vector has too many elements which causes out of memory errors.
Also the xml has links to images which are being downloaded and this makes the situation even worse.
How should I manage the memory in such cases? I was thinking if I could make the images load only when the user scrolls to a given row in the list view.
The problem of out of memory exceptions is the loading of the images. The
Adaptershouldn’t have a problem in populating itself in aListView.In a business application I am loading a
ListViewwith 9000 records with 2TextViews in each row. So I guess the size of the data should not be a problem.Use an
AsyncTaskto fetch the data from the server. Parse and populate the adapter in thedoInBackground()method and then inonPostExecute()set the Adapter in theListView. At first try not to include any of the images so as to make sure that data size is not a problem.If the
ListViewloads correctly then try and find a way to populate the images. There are multiple ways in doing this operation. If the images are the same over and over again you should try to “cache in memory” implementation, otherwise perhaps “lazyloading” from a background thread should do the job just fine.EDIT:
this is a very good post/tutorial on how to lazyload images from a background thread. It is “almost” the same technique like the android market uses when you are viewing all the applications in a
GridView, when at first there are some grey icons and then suddenly the image of the application appear. This is done from a background thread, when the exact row is visible to the user then the background thread fetches it from the internet, populates the list and it caches it in memory so as to be available again in no time. Lazy load of images in ListView