【问题标题】:rewriting a method to use cursor loader重写使用游标加载器的方法
【发布时间】:2013-09-08 19:26:34
【问题描述】:

我有一种方法可以显示数据库中的笔记标题和日期。但是方法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);
}

【问题讨论】:

标签: java android sql cursor


【解决方案1】:

常规的 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() 回调中。如果您做对了所有事情,您的光标就会出现在这里。现在只需使用光标填充您的列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 2013-12-24
    • 2012-10-31
    • 1970-01-01
    相关资源
    最近更新 更多