I have a Listview that contains 100 rows. This is the first time I load all the data from a Webservice. I want to cache that data so that if I open that page I should get it from cache not from the Webservice. How can I do this?
-
use this example,i just got working it.refer this question. [http://stackoverflow.com/questions/15154164/in-android-how-to-implement-cache-in-listview][1] [1]: http://stackoverflow.com/questions/15154164/in-android-how-to-implement-cache-in-listview – jithu Mar 01 '13 at 14:48
4 Answers
If your data is simple enough, just store them in an array and use something like ArrayAdapter to bind the data to the list view.
If your data is more complex, then an SQLite table is probably preferable. In this case use something like SimpleCursorAdapter.

- 1,983
- 2
- 18
- 18
Save your data in a SQLite table and use that as cache next time check if that table exists. If the table exists query that in stead of the webz :-)

- 50,140
- 28
- 121
- 140

- 7,636
- 3
- 32
- 53
You can store the data as a JSON file in your application's internal storage space. I find that this is a much easier approach, as you can easily map the JSON to Model classes using a library like Gson. You would typically follow this approach if the data you have will not be "updated" like you might do in a traditional database(although you are still able to update your JSON data, just differently).

- 26,141
- 19
- 95
- 113
-
I would like to know is Internal storage not accessible even if the phone is rooted? – ingsaurabh Apr 08 '11 at 13:42
-
-
sorry I am not asking that, actually in docs its written that in internal storage are not accessible to other apps and even user I want to know does this condition remains true if the phone is rooted even? – ingsaurabh Apr 08 '11 at 14:11
-
i think its safe to say that on a rooted phone, most data will be public to the user unless you have provided some form of encryption to protect it. – james Apr 08 '11 at 14:54
im assuming since you use the term cache - the data expires every x mins / hours
if the data changes every few mins simply save your webservice response in some local variable (if it only needs to be used for a single listview activity)
or save it in global variable (extend the application class and expose a public variable from here)
if your data changes every few hours then a better idea is to save it in sqlite (if your data needs complex querying / joins etc etc)
or simply save it as a file (either in json or xml format) and check for this file and expiry and if still valid simply decode it instead of calling your webservice again

- 6,810
- 6
- 48
- 56