Android Listview Update/refresh Data Without Closing The App
Solution 1:
You should schedule your DownloadJSON
task to run periodically as follows:
publicvoidcallAsynchronousTask() {
finalHandlerhandler=newHandler();
Timertimer=newTimer();
TimerTaskdoAsynchronousTask=newTimerTask() {
@Overridepublicvoidrun() {
handler.post(newRunnable() {
publicvoidrun() {
try {
DownloadJSONperformBackgroundTask=newDownloadJSON();
performBackgroundTask.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 10000); //execute in every 10 sec
}
This should work however its not the most efficient way as you are requesting the whole data again and again. (There is lots of data being downloaded which is not required).
Instead you should run a task periodically that checks the server if any new updates exist. If server says yes.. then it fetches the complete list.. otherwise it waits for some time and requests for new update again. But for this you need to modify the server API (so you can consider this in the future)
Solution 2:
you can use Google Cloud Messaging(GCM) service in this case. Whenever any changes occur in your web server simply a push notification issued by this server will do for you. Catch it and use it in your app accordingly. You can have a look here
Solution 3:
You just need To set new Adapter To listView ..
newsAdapter = newNewsListViewAdapter(getActivity(),
R.layout.custome_listview_news_item, newsItems);
listViewNews.setAdapter(newsAdapter);
i assume that you have A fragment and you want to update the listview in fragment .. So just make A method in Fragment To update The listview .. And when you have new items .. just request this method within para your data .. Note : you have to set new adapter ..
Post a Comment for "Android Listview Update/refresh Data Without Closing The App"