Handling the Results 处理结果

CursorLoader returns its query results to your implementation ofLoaderCallbacks.onLoadFinished(), in the form of a Cursor. In the callback, you can update your data display, do further processing on theCursor data, and so forth. http://blog.csdn.net/sergeycao

When the loader framework detects changes to data associated with the query, it resets theCursorLoader, closes the current Cursor, and then invokes your implementation ofonLoaderReset(). Use this callback to delete references to the currentCursor; when the loader framework destroys the Cursor, you won't have outstanding references that cause memory leaks.

Handle Query Results

The following two snippets are an example of displaying the results of a query, using aListView backed by a SimpleCursorAdapter.

The first snippet shows the ListView and SimpleCursorAdapter:

// Gets a handle to the Android built-in ListView widget
mListView = ((ListView) findViewById(android.R.id.list));
// Creates a CursorAdapter
mAdapter =
    new SimpleCursorAdapter(
    this,                   // Current context
    R.layout.logitem,       // View for each item in the list
    null,                   // Don't provide the cursor yet
    FROM_COLUMNS,           // List of cursor columns to display
    TO_FIELDS,              // List of TextViews in each line
    0                       // flags
);
// Links the adapter to the ListView
mListView.setAdapter(mAdapter);

The next snippet shows an implementation of onLoadFinished() that moves the query results in the returnedCursor to the SimpleCursorAdapter. Changing theCursor in the SimpleCursorAdapter triggers a refresh of theListView with the new data:

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
{
    /*
     * Move the results into the adapter. This
     * triggers the ListView to re-display.
     */
    mAdapter.swapCursor(cursor);
}

Handle a Loader Reset

The loader framework resets the CursorLoader whenever theCursor becomes invalid. This usually occurs because the data associated with theCursor has changed. Before re-running the query, the framework calls your implementation ofonLoaderReset(). In this callback, make sure to prevent memory leaks by deleting all references to the currentCursor. Once you return from onLoaderReset(), the loader framework re-runs the query.

For example:

public void onLoaderReset(Loader<Cursor> loader)
{
    // Remove the reference to the current Cursor
    mAdapter.swapCursor(null);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值