Reputation: 724
I have ListView
which have data . Data come from server and I want the ListView
to update after every 5 sec. How to do this? I am new to android development. Please help me. Here is my code...
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for (int i = 0; i < peoples.length(); i++) {
JSONObject c = peoples.getJSONObject(i);
String data = c.getString(TAG_DATA);
final String dataaaa = rcdata.getText().toString().trim();
HashMap<String, String> user_data = new HashMap<String, String>();
user_data.put(TAG_DATA, data);
personList.add(user_data);
}
ListAdapter adapter = new SimpleAdapter(
DataSendActivity.this, personList, R.layout.layout_chat,
new String[]{TAG_DATA},
new int[]{R.id.data}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
Upvotes: 0
Views: 1187
Reputation: 20990
Use a Handler
and its postDelayed
method to invalidate the list's adapter as follows:
final Handler handler = new Handler();
handler.postDelayed( new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
handler.postDelayed( this, 5000 );
}
}, 5000 );
You must only update UI in the main (UI) thread.
Upvotes: 1
Reputation: 625
you can see this question and comment How to refresh/Update Serialize Java Object in sharedPreferences. the user want the same. however it is good to use loader with service for that kind of problem calling asyncTask within minute is not good. also theres is a sample project you can check this https://github.com/Michenux/YourAppIdea also available in playstore which check for data changes from server.
Upvotes: 0