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?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
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
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.
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).
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 :-)