Correct workflow to retrieve data from internet and show in a Listview
To me is diffucult to understand which the right workflow is to download json data and show them in a ListView. Currently i am using an AsyncTask to download and parse data that is shown using an ArrayAdapter. The problem is that AsyncTask doesn't survive to activity restarts.
So I am wondering if services are a better solutionbut how to pass data to the ArrayAdapter? Should I always use the db as middle layer to store and retrieve data?
So which is better? AsyncTask in retained fragment or service using db?
Thanks
I use a service to do http data retrieval and storage into sqlite db. Once the service has stored the data (or if there is a problem) I then fire a broadcast. My Fragments / Activities listen for these defined broadcasts and then act appropriately.
I find that this is a very clean solution and avoids the problems of leaking asynctask references on activity teardown / rotation etc.
The most simple solution would be to return your current AsyncTask in onRetainNonConfigurationInstance
or use a Fragment with setRetainInstance(true).
You can access the AsyncTask in inCreate with getLastNonConfigurationInstance
It actually depends on the requirement of what exactly the Activity needs and how long can the data be displayed without hitting the service again.
One approach to the above mentioned problem is, hit the service get the json data and store it in database. Now fetch the data from database and display it in the Activity. You can now implement a service to refresh the data when required. The service will simply hit the web service and update your db content as and when required.
链接地址: http://www.djcxy.com/p/64590.html上一篇: 从AlertDialog中刷新片段