0

Hello I want to refresh the list view in timely manner, Here I am not adding/deleting any rows. I want to refresh because to refresh existing data in a rows.

I am doing this but this is not working for me.

// Update the Message on the screen to help for troubleshooting.
private void updateListView() {
    // Callback to update the message in a second.
    new CountDownTimer(30000, 1000) {

        @Override
        public void onTick(long arg0) {
            adapter.notifyDataSetChanged();
            listView.invalidateViews();
            listView.refreshDrawableState();
        }

        @Override
        public void onFinish() {

        }
    }.start();
}
Mick
  • 1,179
  • 5
  • 14
  • 24

1 Answers1

2

just use a handler and notifyDataSetChanged:

    final Handler handler = new Handler();
    final int REFRESH_EVERY_X_MS = 1000;
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            mAdapter.notifyDataSetChanged();
            handler.postDelayed(this, REFRESH_EVERY_X_MS);
        }
    }, REFRESH_EVERY_X_MS);

and in case you wish to stop (and you should), use handler.removeCallbacks and set the exact same runnable as the parameter.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Will this work for my case because I am not inserting/deleting the rows from the dataset, only updating the data in the existing rows ? – Mick Apr 27 '14 at 10:34
  • the handler also works on the UI thread, so it's ok. just remember to stop the refreshing when you don't need it anymore. – android developer Apr 27 '14 at 10:35
  • No, not working, existing rows are not updating, here I am not inserting/deleting the rows , only want to update the existing rows – Mick Apr 27 '14 at 10:54
  • well unless something is wrong with your adapter, this code should work just fine. if you are new to listView, I highly recommend watching the "the world of listView" lecture. It's old, but it's still one of the best lectures out there. – android developer Apr 27 '14 at 11:15
  • Refresh the listview works when you change the dataadapter here I am not adding/deleting the data so no change in the adapter and so your code is not working – Mick Apr 27 '14 at 11:35
  • My code will work fine, as the getView should be called for all of the views that you've used before on the adapter. you just need to decide what exactly you wish to do there. if you used one of the built in adapter implementation, this won't do any noticeable thing, since it also don't know what to do, and it didn't notice anything that has changed. You can also directly change the views instead of calling notifyDataSetChanged : View v=listView.getChildAt(itemPosition - listView.getFirstVisiblePosition()) – android developer Apr 27 '14 at 11:44
  • I can't chat right now. just write what you need here. – android developer Apr 27 '14 at 11:47