Вопрос

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?

Это было полезно?

Решение

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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top