REST and listviews refresh
In my app,开发者_如何转开发 I have different activities with listviews. The datas come from a server with a REST method, and it's only done once, when I start the application.
The pattern that I'd like to set is to precharge all the listviews with the JSONs that I already have in local, and in parallel, launch a thread that get the new JSONs files with my REST methods, and then update the listviews.
For now, when I start the app, I parse my JSONs files, and build all the lists of objects. I access them later in a static way, from my lists adapters.
So I would like to know the best way to launch this REST thread, and update the listview. Should I use AsyncTask ? A service ? and then, when I update my local JSONs, I have to re-parse them, updates the lists of object, and call in my adapters NotifyDataChanged ?
Thanks
So I found the perfect solution to my problem.
I'm using a sendBroadcast() in the onPostExecute() of my AsyncTask, with broadcastReceiver in all my activities that needed to be notified :
public class ConvenientActivity extends Activity {
...
...
public class RefreshReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MyMainClass.REFRESH_INTENT)) {
mAdapter.refreshList();
}
}
}
}
It's pretty simple, and works perfectly.
So it seems that the question needs details, so I'm gonna explain what the problem is.
When I start my app, I put a splashscreen, and I parse my JSONs files in local that I saved the last time. Then I launch the app, which contains a tab activity with different listviews that use the data that I just got from the JSONs parsing.
At the same time as I launched the app, I start an async task that query a web service, and updates my local JSONs files. Then I parse the files again, and I'd like to notify all the listviews already launched that they have to refresh their data. And I don't really know how to do that.
Here is the code of my async task
class UpdateTask extends AsyncTask{
@Override
protected Object doInBackground(Object... params) {
// Gets all the JSON Files and save them in local
mJsonClient.loadDistantJSON();
// Updates the cache maps and lists
fillsMaps();
fillsLists();
return null;
}
protected void onPostExecute(Object downloadReturn) {
// Notify the listviews
;
}
}
In the others activities that contains a listview, I access the cache list in local in a static way like that :
myAdapter = new MyBaseAdapter(this,
SplashScreen.list,listview);
I'd be glad to give more details if you want.
Thanks
精彩评论