Android: Refresh Listview Every Minute
Solution 1:
Try this approach:
Create an endpoint in your server like the following:
//http://somesite.com/api/data/pull/check
Then, you can easily check this endpoint that returns some value like true or false depending on whether there is new data inserted into the db.
From the result you receive, you can then decide on whether to refresh your data on the phone by making another HTTP request or not. You always want to avoid making unnecessary requests to the server - remember users spend money every time they use their data plan (service).
I, like in the comments above, recommend having a column with a timestamp that you can check so that you only get the newly added data instead of everything!
I hope this gives you a simple idea on how to approach this issue! Good luck!
Solution 2:
android app will not know when you have added/updated data in your table on the server until and unless you call script from app and fetch the data and update in your device.
only if your app has implemented these feature's
- push notification- call Script every time you receive notification.
- XMPP service- used for chat apps(which is not probably answer for your question right now)
here is my suggestion for you
From server side:
create timestamp field in your table on server. update it with current timestamp value every time you do changes(i.e update/add) in the table.and when when that script is called send it across in json and make your app save it in sqlite along with data.
server will compare for timestamp posted by app everytime with the saved timestamp in the server for new data.
from client side:
- for fist time timestamp from app will be 0. server will check it and send the whole data along with the timestamp saved during changes in table. save the data along with time stamp . second time when the script is called App will be sending the timestamp that was last saved.
with all this your app will not come to know still if new data is added until you call script and check. but atleast it will come to know if new data is received or not and whether to refresh ur screen
now comes script calling part from client side that is executing of assynch task, do it using handler to execute assynch class every minute
finalHandlertimerHandler=newHandler();
Runnable timerRunnable;
timerRunnable = newRunnable() {
@Overridepublicvoidrun() {
newFetchRowNumAsync(context).execute(url);
timerHandler.postDelayed(timerRunnable, 60000); // run every minute
}
};
and unregister it in onDestroy()
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
timerHandler.removeCallbacks(timerRunnable);
}
Post a Comment for "Android: Refresh Listview Every Minute"