I have implemented a simple infinite scrolling GridView on Android. I have the following components:
- a GridView with a onScrollListener, whenever the GridView is scrolled to the very bottom, I kick off an AsyncTask to load more items into my Adapter.
- an adapter that appends the items fetched from the AsyncTask, and fires notifyDataChanged() so the GridView can refresh itself.
This seems to work fine except in this case the adapter is growing indefinitely...I mean I keep appending items to this adapter whenever the user scrolls to the end, this seems to be a memory problem.
What's the proper way of implementing an endless adapter? I am looking for concepts, not a third party jar that does it for me.
Thanks
solution
The marked answer has the right theory. My implementation of it backs up the loaded data into a sqllite database, so the overall flow is: download data from the internet in a thread, parse and store the data into database in a thread, fire notifyDataChanged on UI thread.
The adapter's getCount() method simply does a select count(id) from the database, and it retrieves a batch of objects at once from the database into memory for the adapter to use.