使用SimpleCursorAdapter更新Android ListView
嘿,我使用listview来演示存储在数据库中的条目。我还有一个EditText元素和一个按钮,用于将EditText的内容添加到数据库中。要将视图绑定到数据库内容,我使用SimpleCursorAdapter和以下填充函数:使用SimpleCursorAdapter更新Android ListView,android,listview,android-listview,simplecursoradapter,Android,Listview,Android Listview,Simplecursoradapter,嘿,我使用listview来演示存储在数据库中的条目。我还有一个EditText元素和一个按钮,用于将EditText的内容添加到数据库中。要将视图绑定到数据库内容,我使用SimpleCursorAdapter和以下填充函数: private void populate() { cursor = dbAdapter.getAllItems(); startManagingCursor(cursor); String[] from = new String[] { DBA
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);
}
如果我添加了一个新条目,通过单击按钮我想刷新listview,但这只适用于填充函数,而不适用于公共适配器函数notifyDataSetChanged()代码>。我是否有错误,或者这是刷新列表视图的正确方法?您是否看到、尝试过该方法,或者只是尝试再次调用
我遇到了一个类似的问题,我无法更新我的列表,我所做的只是创建了一个refreshListView()方法。现在,您可以从onCreate()开始调用它,并且用户可以随时向数据库添加内容。它所做的只是将listview重新绑定到一个游标。考虑到所有不推荐的方法(requery()),以及notifyDataSetChanged()的问题,我认为这是最简单的方法。请参考此链接……它的效果非常好
对于滚动上的动态列表视图,我从数据库中添加了新项。。
我确实犯了错误。。
我每次都为同一个SimpleCrsorAdapter分配新的适配器。
而不是创建新的适配器。
只用
adapter.changecursor(newcursorValue);
adapter.notifydatasetChanged();
lsMedicine1.setSelectionFromTop(lsMedicine1.getLastVisiblePosition()-20, 0);
您需要在适配器上的notifyDataSetChanged()
之前调用swapcursor()
。只需提一下:“requery()”已被弃用。我知道,我没有把那句话说对,重新措辞:>嘿,杰克,谢谢你的回复,我发现我忘了说上面的代码是我的populate()
函数,我在onCreate()
方法中调用它,如果我点击“Add”-按钮。我只想知道调用此方法而不是调用基本适配器类的公共notifyDataSetChanged()
方法是否是一个好的解决方案。哦,我明白了,现在太早了,我还没有喝咖啡。是的,那应该很好,我已经用了一段时间了,效果很好。呵呵……好的,没问题,有时候没有咖啡或红牛,我也有同样的问题,事实上这是我的错。但是谢谢你的帮助。