与SimpleCursorAdapter Android的ListView控件更新控件、SimpleCursorAdapter、Android、ListView

2023-09-06 14:07:39 作者:男人你有什么资格说累

嘿,我用一个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?

推荐答案

你见过this,尝试了swap光标方法,或尝试只是简单调用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.