Android 使用CursorLoader執(zhí)行查詢?nèi)蝿?wù)

2018-08-02 18:24 更新

編寫:kesenhoo - 原文:http://developer.android.com/training/load-data-background/setup-loader.html

CursorLoader通過ContentProvider在后臺(tái)執(zhí)行一個(gè)異步的查詢操作,并且返回?cái)?shù)據(jù)給調(diào)用它的Activity或者FragmentActivity。這使得Activity或者FragmentActivity能夠在查詢?nèi)蝿?wù)正在執(zhí)行的同時(shí)繼續(xù)與用戶進(jìn)行其他的交互操作。

定義使用CursorLoader的Activity

為了在Activity或者FragmentActivity中使用CursorLoader,它們需要實(shí)現(xiàn)LoaderCallbacks<Cursor>接口。CursorLoader會(huì)調(diào)用LoaderCallbacks<Cursor>定義的這些回調(diào)方法與Activity進(jìn)行交互;這節(jié)課與下節(jié)課會(huì)詳細(xì)介紹每一個(gè)回調(diào)方法。

例如,下面演示了FragmentActivity如何使用CursorLoader。

public class PhotoThumbnailFragment extends FragmentActivity implements
        LoaderManager.LoaderCallbacks<Cursor> {
...
}

初始化查詢

為了初始化查詢,需要調(diào)用LoaderManager.initLoader()。這個(gè)方法可以初始化LoaderManager的后臺(tái)查詢框架。你可以在用戶輸入查詢條件之后觸發(fā)初始化的操作,如果你不需要用戶輸入數(shù)據(jù)作為查詢條件,你可以在onCreate()或者onCreateView()里面觸發(fā)這個(gè)方法。例如:

// Identifies a particular Loader being used in this component
private static final int URL_LOADER = 0;
...
/* When the system is ready for the Fragment to appear, this displays
 * the Fragment's View
 */
public View onCreateView(
        LayoutInflater inflater,
        ViewGroup viewGroup,
        Bundle bundle) {
    ...
    /*
     * Initializes the CursorLoader. The URL_LOADER value is eventually passed
     * to onCreateLoader().
     */
    getLoaderManager().initLoader(URL_LOADER, null, this);
    ...
}

Note: getLoaderManager()僅僅是在Fragment類中可以直接訪問。為了在FragmentActivity中獲取到LoaderManager,需要執(zhí)行getSupportLoaderManager().

開始查詢

一旦后臺(tái)任務(wù)被初始化好,它會(huì)執(zhí)行你實(shí)現(xiàn)的回調(diào)方法onCreateLoader()。為了啟動(dòng)查詢?nèi)蝿?wù),會(huì)在這個(gè)方法里面返回CursorLoader。你可以初始化一個(gè)空的CursorLoader然后使用它的方法來定義你的查詢條件,或者你可以在初始化CursorLoader對(duì)象的時(shí)候就同時(shí)定義好查詢條件:

/*
* Callback that's invoked when the system has initialized the Loader and
* is ready to start the query. This usually happens when initLoader() is
* called. The loaderID argument contains the ID value passed to the
* initLoader() call.
*/
@Override
public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle)
{
    /*
     * Takes action based on the ID of the Loader that's being created
     */
    switch (loaderID) {
        case URL_LOADER:
            // Returns a new CursorLoader
            return new CursorLoader(
                        getActivity(),   // Parent activity context
                        mDataUrl,        // Table to query
                        mProjection,     // Projection to return
                        null,            // No selection clause
                        null,            // No selection arguments
                        null             // Default sort order
        );
        default:
            // An invalid id was passed in
            return null;
    }
}

一旦后臺(tái)查詢?nèi)蝿?wù)獲取到了這個(gè)Loader對(duì)象,就開始在后臺(tái)執(zhí)行查詢的任務(wù)。當(dāng)查詢完成之后,會(huì)執(zhí)行onLoadFinished()這個(gè)回調(diào)函數(shù),關(guān)于這些內(nèi)容會(huì)在下一節(jié)講到。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)