嘿,我使用列表视图来演示存储在数据库中的条目。我还有一个 EditText 元素和一个将 EditText 的内容添加到数据库中的按钮。要将视图绑定到数据库内容,我使用 SimpleCursorAdapter 和以下填充函数:
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);
}
如果我添加了一个新条目,通过单击按钮我想刷新列表视图,但这仅适用于填充功能,不适用于通用适配器功能notifyDataSetChanged();
。我是否有错误或者这是刷新列表视图的正确方法?
你见过吗this,尝试了交换光标方法,或者尝试只是简单地调用设置适配器() again?
我遇到了类似的问题,我无法更新列表,我所做的只是创建一个刷新列表视图()方法。现在,您可以从 onCreate() 中调用它,并且任何时候用户向数据库添加内容时都可以调用它。它所做的只是将列表视图重新绑定到光标。考虑到所有已弃用的方法 (requery()) 以及 notificationDataSetChanged() 的问题,我认为这是最简单的方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)