重写一个使用游标加载器的方法

时间:2013-09-08 10:39:18

标签: 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(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);
}

1 个答案:

答案 0 :(得分:0)

常规CursorLoader基于Android的ContentProvider - 所以如果你不使用它,你将不得不编写自己的Loader。这是你可以做到的一种方式:

https://github.com/commonsguy/cwac-loaderex/blob/master/src/com/commonsware/cwac/loaderex/acl/AbstractCursorLoader.java

从那时起,它非常直接。 您的活动/片段可以是LoaderManager的回调 - 它需要实现LoaderCallbacks接口。

使用getLoaderManager()。initLoader(this,null,LOADER_ID)来初始化数据加载,而不是运行查询。这是一个例子:http://developer.android.com/reference/android/app/LoaderManager.html

现在,数据将在onLoadFinished()回调中。如果你做的一切正确,你会把光标放在这里。现在只需使用光标填充列表即可。