使用SimpleCursorAdapter进行Android ListView更新

Sig*_*sen 5 android listview android-listview simplecursoradapter

嘿,我使用listview来显示存储在数据库中的条目.我还有一个EditText元素和一个按钮,它将EditText的内容添加到数据库中.要将视图绑定到数据库内容,我使用SimpleCursorAdapter和以下populate函数:

private void populate() {
    cursor = dbAdapter.getAllItems();
    startManagingCursor(cursor);

    String[] from = new String[] { DBAdapter.KEY_TASK };
    int[] to = new int[] { android.R.id.text1 };

    // Now create an array adapter and set it to display using our row
    cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to);
    list.setAdapter(cursorAdapter);
}
Run Code Online (Sandbox Code Playgroud)

如果我添加了一个新条目,通过单击我要刷新列表视图的按钮,但这只适用于populate函数而不适用于公共适配器函数notifyDataSetChanged();.我有错误或者这是刷新列表视图的正确方法吗?

Jac*_*ack 9

你有没有看过这个,试过交换游标方法,或者只是简单地再次调用setAdapter()

我有一个类似的问题,我无法让我的列表更新,我做的只是创建一个refreshListView()方法.现在你可以从你的onCreate()开始调用它,并在用户向数据库添加内容时随时调用.它只是将listview重新绑定到游标.使用所有弃用方法(requery())以及notifyDataSetChanged()的问题,我认为这是最简单的方法.


归档时间:

查看次数:

11181 次

最近记录:

9 年,5 月 前