Issue
I have following scenario:
- I develop an Android Application which has a ListView in the MainActivity
- The data for populating the ListView comes from a URL returning JSON data
- I want to "cache" the ListView items as long as possible (depending on the cache control header of the URL resource), which means that I do not want to reload the ListView data (from the external URL) each time the user goes back to the MainActivity
I found this article: Android AsyncTask ListView - JSON|Surviving w/ Android which is good, but not excatly what I want. I do not want to execute the AsyncTask each time the user comes back to the MainActivity.
I thought of storing the ListView items in a globally available parameter of the Application. This parameter would then be some sort of custom container which holds the ListItems and the timestamp at which they expire. Then it would be possible to check in the "doInBackground" method of the AsyncTask if the ListView items are valid. If yes they are returned, if not they are loaded from URL and saved to the global variables.
My question: Does this solution make sense? How would you solve this? Any ideas on making this better?
Solution
Well, yours make sense; however, I'd like to add something.
For coding part,
- Cache the JSON each time your request done successfully. You probably gonna need to use
LruCache
that should be defined in your customApplication
class. Each key/value pair store the data model after parsing JSON. For example,cache.put(model.id, model)
.
For UX part,
You don't need to update JSON every time, but provide an action trigger to do it. Some common ways is:
- Provide a refresh button somewhere on screen? (Could be an item on
ActionBar
...) - Application starts execution.
- Users pull down the
ListView
to refresh content. - ...
- Provide a refresh button somewhere on screen? (Could be an item on
Answered By - Pete Houston
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.