与SimpleCursorAdapter Android的ListView控件更新 [英] Android ListView update with SimpleCursorAdapter

查看:168
本文介绍了与SimpleCursorAdapter Android的ListView控件更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我用一个ListView用于展示其存储在数据库中的条目。我也有一个元素的EditText和一个按钮,添加的EditText内容到数据库中。要将视图绑定到数据库的内容我使用SimpleCursorAdapter及以下填充功能:

Hey i use a listview for demonstrate entries which are stored in a database. I also have a EditText element and a button which adds the content of the EditText into the Database. To bind the view to the database content i use the SimpleCursorAdapter and following populate function:

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(); 。我有一个bug或者,这是正确的方式来刷新列表视图?

If i added a new entry, by clicking on the button i want to refresh the listview but this only works with the populate function and not with the common adapter function notifyDataSetChanged();. Do i have a bug or is this the right way to refresh a listview?

推荐答案

你见过<一个href="http://stackoverflow.com/questions/1985955/android-simplecursoradapter-not-updating-with-database-changes">this,尝试了<一href="http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#swapCursor%28android.database.Cursor%29">swap光标方法,或尝试只是简单调用<一href="http://stackoverflow.com/questions/5113180/android-refresh-listview-using-listadapter-and-simplecursoradapter">setAdapter()再次?

Have you seen this, tried the swap cursor method, or tried just simply calling setAdapter() again?

我有一个类似的问题,我不能让我的名单进行更新,而我所做的只是创建一个refreshListView()方法。现在,你可以从你的onCreate()调用这个最初,随时随地用户添加的东西到数据库。它所做的就是重新绑定列表视图,以光标。与所有的德precating方法(重新查询()),以及问题notifyDataSetChanged(),我决定这是最简单的方法。

I had a similar issue where I could not get my list to update, and what I did was just create a refreshListView() method. Now you can call this initially from your onCreate(), AND anytime a user adds something to the DB. All it does is re-bind the listview to a cursor. With all the deprecating methods (requery()), and issues with notifyDataSetChanged(), I decided this was the easiest way.

这篇关于与SimpleCursorAdapter Android的ListView控件更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆