Java 重写使用游标加载程序的方法

Java 重写使用游标加载程序的方法,java,android,sql,cursor,Java,Android,Sql,Cursor,我有一个从数据库显示注释标题和日期的方法。但是,不推荐使用方法startManagingCursor。有没有什么方法可以用游标加载器重新编写 private void fillData() { // Get all of the notes from the database and create the item list Cursor notesCursor = mDbHelper.fetchAllNotes(); startManagingCursor(notesC

我有一个从数据库显示注释标题和日期的方法。但是,不推荐使用方法
startManagingCursor
。有没有什么方法可以用游标加载器重新编写

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


    String[] from = new String[] { NotesDbAdapter.KEY_TITLE ,NotesDbAdapter.KEY_DATE};
    int[] to = new int[] { R.id.text1 ,R.id.date_row};

    // Now create an array adapter and set it to display using our row
    SimpleCursorAdapter notes =
        new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
    setListAdapter(notes);
}

常规的游标加载程序是基于Android的ContentProvider的——因此,如果您不使用它,您必须编写自己的加载程序。这里有一种方法可以做到:

从这一点上讲,这是非常直接的。 您的活动/片段可以是LoaderManager的回调-它需要实现LoaderCallbacks接口

不要运行查询,而是使用getLoaderManager().initLoader(这个,null,LOADER_ID)初始化数据加载。下面是一个例子:

现在,数据将在onLoadFinished()回调中。如果你做的每件事都是对的,你的光标就会在这里。现在只需使用光标填充列表

你看了吗