【问题标题】:How can I refresh ListView?如何刷新 ListView?
【发布时间】:2015-03-22 11:24:44
【问题描述】:

如何刷新列表视图?这里如何使用 notifyDatasetchanged 来刷新来刷新列表视图?

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);
}

【问题讨论】:

  • 欢迎。请用更多细节标记您的问题,例如语言,也许是您正在使用的平台
  • 不太清楚你在问什么。只要您的模型发生变化,只需致电notifyDatasetChanged()。您也可以在每次数据更改时仅覆盖附加到ListAdapter 的当前模型,但使用来自cursor 的新数据再次调用setListAdapter(listAdapter)

标签: android android-listview simplecursoradapter


【解决方案1】:

就像之前的回复所说的那样,只要您预计数据已更改,或者您有其他方法可以通过回调等方式检测到这一点,您就应该调用 notifyDataSetChanged()。重新创建适配器似乎是在浪费时间和内存,具体取决于关于您存储了多少数据,因为所有数据都必须在某个时候被复制。

【讨论】:

    【解决方案2】:

    函数notifyDataSetChanged 属于适配器。因此,当您的数据模型发生更改时,您必须从适配器调用它。 示例:

    onEvent() {listAdapter.notifyDataSetChanged();}
    

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      相关资源
      最近更新 更多