CursorLoader解析

在应用程序中使用CursorLoader所需的元素

实例化一个Loader:

1 getLoaderManager().initLoader(0, null, this);

第一个参数:用来区分loader实例的唯一ID

第二个参数:可选参数

第三个参数:一个实现了LoaderManager.LoaderCallbacks接口的实例

 

LoaderManager.LoaderCallbacks包含三个方法:

onCreateLoader() 初始化并且返回一个给定ID的Loader

onLoadFinished()  在之前创建的loader完成之后调用

onLoaderReset() 在之前创建的loader reset时调用

 

onCreateLoader(int id,Bundle args)

在下面这个例子中,我们用到了CursorLoader,在构造函数中有以下参数:

  • uri — The URI for the content to retrieve.
  • projection — A list of which columns to return. Passing null will return all columns, which is inefficient.
  • selection — A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.
  • selectionArgs — You may include ?s in the selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.
  • sortOrder — How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.
复制代码
 // If non-null, this is the current filter the user has provided.
String mCurFilter;
...
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // This is called when a new Loader needs to be created.  This
    // sample only has one Loader, so we don't care about the ID.
    // First, pick the base URI to use depending on whether we are
    // currently filtering.
    Uri baseUri;
    if (mCurFilter != null) {
        baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
                  Uri.encode(mCurFilter));
    } else {
        baseUri = Contacts.CONTENT_URI;
    }

    // Now create and return a CursorLoader that will take care of
    // creating a Cursor for the data being displayed.
    String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
            + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
            + Contacts.DISPLAY_NAME + " != '' ))";
    return new CursorLoader(getActivity(), baseUri,
            CONTACTS_SUMMARY_PROJECTION, select, null,
            Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
复制代码

 

onLoadFinish(Loader<Cursor> loader,Cursor data)

这个方法会确保在释放此loader提供的最后的数据之前调用(原文:This method is guaranteed to be called prior to the release of the last data that was supplied for this loader.)

loader会自动释放应用不再使用到的data,例如,如果这个data是一个来自CursorLoader的cursor,不应该使用close()来释放它,如果这个cursor被放到了一个CursorAdapter,应该使用swapCursor方法来确保旧的Cursor不会被关闭,例如:

复制代码
// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;
...

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Swap the new cursor in.  (The framework will take care of closing the
    // old cursor once we return.)
    mAdapter.swapCursor(data);
}
复制代码

 

onLoaderReset(Loader<Cursor> loader)

这个方法在已创建的loader reset的时候调用,会使得loader的数据不可用

下面的实现调用swapCursor(null):

 

复制代码
// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;
...

public void onLoaderReset(Loader<Cursor> loader) {
    // This is called when the last Cursor provided to onLoadFinished()
    // above is about to be closed.  We need to make sure we are no
    // longer using it.
    mAdapter.swapCursor(null);
}
复制代码

 

下面是一个完整的例子,它使用一个Fragment读取联系人,必须在manifest文件中添加READ_CONTACTS权限

  View Code

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值