2

I'm making an adapter that it underline data loads from a web service and each time the user scrolls down it loads more data and adds it to the ArrayList behind that adapter. The data contains image URLs which I then lazy load in my getView method.

  • Is there a better solution for that?
  • And how do I properly cache those images?
  • And can I retain the data of the adapter when the user rotates the device? So no need to load them again from the beginning?

I use fragment to display that list?

Peter Monks
  • 4,219
  • 2
  • 22
  • 38
user1320899
  • 121
  • 1
  • 3

1 Answers1

2

I'll try to answer all three questions one by one.

is there a beter solution for that? (other then lazy-loading)

That depends. The nice part about lazy-loading is, that you don't need to have all data right at the start when you display the component. The problem often is, how to get new data, as it is needed.

So, when loading the new data from the network, it is really a question of how big the chunks of loaded data are. I would not load one new entry after the next one, but load e.g. 20 new ones, when the end of the list has been reached.

Of course this is just a random idea for a number, you'll need to find the perfect point between speed and usability for your case.

So when you are loading chunks of data from your web-service, which are delivered fast enough, it really is a cool concept. But if you're just reading one entry after another (and therefore, the user almost always waits for new data), this is really just annoying.

And how to proper cache the images?

There are multiple possible solutions available, some of those are:

  • Using the system-cache, like the browser (seems to be the finest).
  • Implementing your own system which uses device-storage (e.g. the SD-Card or the internal application storage (the first one is preferred)).

You might also like the contents of this question (and it's accepted answer), giving you some general advises on memory-management.

can i retain the data of the adapter when the user rotates the device?

Old, deprecated anser:

Yes, you can use the "instance state" of the Activity to do this. Basically, the onSaveInstanceState()-method from the Activity allows you to populate a given Bundle with state-information, which is then passed to the onCreate()-method.

The data you would save in the Bundle would be (for example) the current position of the list or maybe even the list itself (depending on the size).

Yes, use the Loaders API. It is also available in the compatibility library.

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • I want to use a fragment to display the list how can i retian the adapter with it, .? U would say use onSaveInstanceState but i call setRetain to true in the onCreate method and this make any Bundle always null since the fragment is not killed – user1320899 Apr 10 '12 at 23:51
  • @user1320899 this might help you: http://stackoverflow.com/questions/5412746/android-fragment-onrestoreinstancestate – Lukas Knuth Apr 10 '12 at 23:59