Setting Up the Loader 设置装载机

本文介绍如何在Android应用中使用CursorLoader加载数据。为了设置加载器框架,你需要扩展FragmentActivity并实现LoaderCallbacks<Cursor>接口。文章还介绍了如何初始化加载器框架,并通过示例代码展示了如何获取LoaderManager实例及初始化CursorLoader。

You create a CursorLoader within a loader framework. To set up the framework, you implement theLoaderCallbacks<Cursor> as part of an Activity. In addition, to provide compatibility compatible with platform versions starting with Android 1.6, you must extend theActivity with the FragmentActivity class. http://blog.csdn.net/sergeycao

Note: A Fragment is not a prerequisite forCursorLoader. As a convenience, the support library classFragmentActivity contains the fragment and the loader frameworks, but they are completely independent of each other.

Before you can use the loader framework, you need to initialize it. To do this, retrieve aLoaderManager object and call its initLoader() method.

If you do use one or more Fragment objects in an Activity, the LoaderManager you retrieve is available to all of them.

Extend an Activity

To set up an Activity subclass to contain a CursorLoader, extend the subclass with must extend FragmentActivity, which provides the loader framework, and implement theLoaderCallbacks<Cursor> interface, which specifies method signatures that the loader framework uses to interact with theActivity.

For example:

public class DisplayActivity extends FragmentActivity
        implements LoaderManager.LoaderCallbacks<Cursor>

Retrieve a LoaderManager

To get an instance LoaderManager for use in your Activity, call FragmentActivity.getSupportLoaderManager() at the beginning of theonCreate() method. For example:

private LoaderManager mLoaderManager;
public void onCreate() {
...
mLoaderManager = this.getSupportLoaderManager();

Initialize the Loader Framework

Once you have the LoaderManager object, initialize it by callinginitLoader(). For example:

// CursorLoader instance identifier
public static final int URL_LOADER = 0;
...
// Initializes the CursorLoader
getSupportLoaderManager().initLoader(URL_LOADER, null, this);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值