How to cache list data in android?

I have a Listview containing 100 rows. This is the first time I download all the data from the Webservice. I want to cache this data so that if I open this page, I have to get it from the cache not from Webservice. How can i do this?

+6
source share
4 answers

If your data is simple enough, just store it in an array and use something like an ArrayAdapter to bind the data to a list.

If your data is more complex, it is best to use a SQLite table. In this case, use something like SimpleCursorAdapter .

+3
source

Save your data in a SQLite table and use it as a cache next time if this table exists. If there is a query in the table, which instead of webz :-)

+1
source

You can save data as a JSON file in your application internal storage space . I believe this is a much simpler approach since you can easily map JSON classes to Model using a library like Gson . You will usually follow this approach if the data you have will not be β€œupdated”, as you could in a traditional database (although you can still update JSON data, otherwise).

+1
source

im assumed as you use the term cache - data expires every x minutes / hours

if the data changes every few minutes, just save your webservice response in some local variable (if you need to use it for only one activity of the listview view)

or save it in a global variable (extend the application class and display the public variable here)

if your data changes every few hours, it is better to understand how to save it in sqlite (if your data requires complex query / union, etc.)

or just save it as a file (in json or xml format) and check this file and its expiration date and, if it is still valid, just decrypt it instead of calling your web service again

0
source

Source: https://habr.com/ru/post/885509/


All Articles