繁体   English   中英

使用SimpleCursorAdapter进行Android ListView更新

[英]Android ListView update with 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);
}

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

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

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

请参考此链接...它的功能就像魅力一样

更新SimpleCursorAdapter,同时在ListView中保持滚动位置

对于滚动的动态列表视图我从数据库添加了新项目..我在这里错了..我为同一个simplecursoradapter每次分配新的适配器。 而不是创建新的适配器。 只是用

adapter.changecursor(newcursorValue);
adapter.notifydatasetChanged();
 lsMedicine1.setSelectionFromTop(lsMedicine1.getLastVisiblePosition()-20, 0);

你需要调用swapcursor()之前notifyDataSetChanged()的适配器上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM