Alex.M.K
Alex.M.K

Reputation: 99

AsyncTask and listview - how to avoid loading all data every time

Im loading data from a web server and presenting the result in a listview. The code is working fine, but the list is empty until the data is done loading from AsyncTask and everytime the user leaves the screen and comes back the list has to load again. I have multiple lists across my application where i do the same approach and it seems clumsy and wrong when all my lists has to load the entire data each and everytime.

So as it is right now im thinking about storing the data somewhere so i can show the old data until I have gotten newer data from the server and then update the list with the new data.

So my first question would be, where should i store such data? In a file? or should i create an SQLite database and store the list objects there?

Since I have multiple lists across my application i was thinking the database approach would be the best solution. Then i could get the latest inserted id for a given object/table and check the server whether there is new data to retrieve.

I have also read about the AsynTaskLoader class which seems to do what im looking for, but i can't really find any good guides to follow. And since i already have my AsyncTask fully working it might be too much unnecessary work to implement the loader.

So yea what would be the recommended approach to this problem?

The data i get back from the server is a JSONArray and im using SimpleAdapter to create my lists.

Let me know if you need to see any of the code.

Thanks in advance!

Upvotes: 1

Views: 868

Answers (3)

gunheekim
gunheekim

Reputation: 21

I'm not sure it is good answer but, i solved by this method.

it is a setting variable identifier by unique...

yeah it is very simple respones your such a dick problem but it is really important thing many programmers missed and in android, AsyncTask is used recognize same variable identifier as same this even if it is exist another fragment

keep in your mind and try this thing

Upvotes: 0

VJ Vélan Solutions
VJ Vélan Solutions

Reputation: 6554

Cache the http response that includes your JSON data: Use this as reference.

Also, it's always a good idea to put a progress dialog (the one that spins indeterminate amount of time) and then dismiss it when the data is ready to be displayed.

Upvotes: 1

viplezer
viplezer

Reputation: 5599

You have to save the state of your Activity (or Fragment) and reload it when the user switch back to it. http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState

Upvotes: 0

Related Questions