I want to call adapter.notifyDataSetChanged()
from another thread. I read that I should use an AsyncTask
and do the adapter.notifyDataSetChanged()
in post execute.
I must execute the AsyncTask
every 5 seconds only on the current activity (might be parent or child activity) because only one activity can do the asynctask at the same time.
Should I create a TimerTask
which executes the AsyncTask
every 5 seconds, stop it when I start another activity and start it back in onResume
?
Here is my code for the thread which updates the ListView
of the current Activity.
private void runEventHandler() {
new Thread() {
public void run() {
while (true) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
users.add(new User(10, "a", false));
adapter.notifyDataSetChanged();
}
});
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
Now I must be able to update the child activities' ListViews
when a new User is added.