Issue
How can I constantly update list items in a list view in Android?
I have an activity that monitors the progress of ongoing transactions. When I load the progress view it captures the state at the moment of creation, but naturally I want to update it.
I tried following this answer, but this creates a code block which is executed constantly even if the activity is not in focus.
Perhaps there is some kind of dynamic list view adaptor that I am unaware of?
Solution
The linked answer is mostly fine, just tweak it a little bit to prevent it running after onPause()
:
private boolean mRunning;
Handler mHandler = new Handler();
Runnable mUpdater = new Runnable() {
@Override
public void run() {
// check if still in focus
if (!mRunning) return;
// upadte your list view
// schedule next run
mHandler.postDelayed(this, 500); // set time here to refresh views
}
};
@Override
protected void onResume() {
super.onResume();
mRunning = true;
// start first run by hand
mHandler.post(mUpdater);
}
@Override
protected void onPause() {
super.onPause();
mRunning= false;
}
Answered By - flx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.