0

我刚刚开始进行 Android 开发,并且一直在构建一个使用 a ListActivity、 aSQLiteDatabase和 a的简单应用程序SimpleCursorAdapter

在 Android 开发者网站上,有一个示例项目演示了SimpleCursorAdadpter. 查看实现,每当底层数据库由于某些用户操作而被修改时,ListActivity显式调用以下函数来“刷新”列表:

private void fillData() {
    // Get all of the rows from the database and create the item list
    mNotesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(mNotesCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{NotesDbAdapter.KEY_TITLE};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.text1};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
        new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
    setListAdapter(notes);
}

看起来SimpleCursorAdapter每次都会创建一个新的并使用setListAdapter(). 这是最好/最干净的实现吗?这是在 Android 网站上的事实赋予了它很大的可信度,但是我查看了CursorAdapter文档,发现有一种changeCursor()方法似乎比上述方法更容易实现,但我只是不确定是什么那可能看起来像。

也许我只是无所事事,但来自 C/C++ 世界,看到每次从数据库中插入/删除一行时创建的这个“新”对象似乎有点笨拙。

4

1 回答 1

0

是的你是对的。你可以看到很多次。对于总是在 onResume 中创建列表时可能不会产生任何问题的小列表。但这不是好的风格。您可以使用 cursor.changeCursor() 或 adapter.notifyDataSetChanged()。

于 2011-07-10T12:40:41.270 回答