0

How to refresh list view? How can notifyDatasetchanged be used here to refresh to refresh the list view?

private void updateUI() {
    helper = new TaskDBHelper(MainActivity.this);
    SQLiteDatabase sqlDB = helper.getReadableDatabase();
    Cursor cursor = sqlDB.query(TaskContract.TABLE,
            new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK},
            null,null,null,null,null);

    SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(
            this,
            R.layout.task,
            cursor,
            new String[]{TaskContract.Columns.TASK},
            new int[]{R.id.taskTextView},
            0
    );
    this.setListAdapter(listAdapter);
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Shilpa M
  • 1
  • 4
  • Welcome. Please tag your question with more specifics, like the language, perhaps the platform you're working on – Wain Mar 22 '15 at 11:30
  • 1
    It's not really clear what you're asking. Just call `notifyDatasetChanged()` whenever you're model is changing. You can also just overwrite the current model attached to the `ListAdapter` every time the data changes, but calling `setListAdapter(listAdapter)` once again with the new data from the `cursor`. – Darwind Mar 22 '15 at 15:46
  • possible duplicate of [How to refresh Android listview?](http://stackoverflow.com/questions/2250770/how-to-refresh-android-listview) – Lokesh Jun 01 '15 at 12:23

2 Answers2

0

Like the previous responses said, you should just call notifyDataSetChanged() whenever you anticipate the data changed or if you have some other way of detecting this through a callback etc. To recreate the adapter seems like a waste of time and memory depending on how much data you have stored as all the data will have to be copied at some point.

andrew749
  • 171
  • 1
  • 10
0

function notifyDataSetChanged is belong to adapter. So you have to call it from adapter when your data model has changed. Example :

onEvent() {listAdapter.notifyDataSetChanged();}
ThaiPD
  • 3,503
  • 3
  • 30
  • 48