本文介绍了没有 ContentProvider 的 CursorLoader 使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android SDK 文档说 startManagingCursor() 方法已弃用:

Android SDK documentation says that startManagingCursor() method is depracated:

此方法已弃用.将新的 CursorLoader 类与 LoaderManager 一起使用;这也可以通过 Android 兼容包在旧平台上使用.此方法允许 Activity 根据 Activity 的生命周期为您管理给定的 Cursor 生命周期.也就是说,当活动停止时,它会自动调用给定 Cursor 上的deactivate(),当它稍后重新启动时,它会为你调用requery().当活动被销毁时,所有托管的游标将自动关闭.如果您的目标是 HONEYCOMB 或更高版本,请考虑改用 LoaderManager,可通过 getLoaderManager() 获得

所以我想使用CursorLoader.但是,当我在 CursorLoader 的构造函数中需要 URI 时,如何在没有 ContentProvider 的情况下将它与自定义 CursorAdapter 一起使用?

So I would like to use CursorLoader. But how can I use it with custom CursorAdapter and without ContentProvider, when I needs URI in constructor of CursorLoader?

推荐答案

我写了一个 simple CursorLoader不需要内容提供者:

I wrote a simple CursorLoader that does not need a content provider:

import android.content.Context;
import android.database.Cursor;
import android.support.v4.content.AsyncTaskLoader;

/**
 * Used to write apps that run on platforms prior to Android 3.0. When running
 * on Android 3.0 or above, this implementation is still used; it does not try
 * to switch to the framework's implementation. See the framework SDK
 * documentation for a class overview.
 *
 * This was based on the CursorLoader class
 */
public abstract class SimpleCursorLoader extends AsyncTaskLoader<Cursor> {
    private Cursor mCursor;

    public SimpleCursorLoader(Context context) {
        super(context);
    }

    /* Runs on a worker thread */
    @Override
    public abstract Cursor loadInBackground();

    /* Runs on the UI thread */
    @Override
    public void deliverResult(Cursor cursor) {
        if (isReset()) {
            // An async query came in while the loader is stopped
            if (cursor != null) {
                cursor.close();
            }
            return;
        }
        Cursor oldCursor = mCursor;
        mCursor = cursor;

        if (isStarted()) {
            super.deliverResult(cursor);
        }

        if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
            oldCursor.close();
        }
    }

    /**
     * Starts an asynchronous load of the contacts list data. When the result is ready the callbacks
     * will be called on the UI thread. If a previous load has been completed and is still valid
     * the result may be passed to the callbacks immediately.
     * <p/>
     * Must be called from the UI thread
     */
    @Override
    protected void onStartLoading() {
        if (mCursor != null) {
            deliverResult(mCursor);
        }
        if (takeContentChanged() || mCursor == null) {
            forceLoad();
        }
    }

    /**
     * Must be called from the UI thread
     */
    @Override
    protected void onStopLoading() {
        // Attempt to cancel the current load task if possible.
        cancelLoad();
    }

    @Override
    public void onCanceled(Cursor cursor) {
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
    }

    @Override
    protected void onReset() {
        super.onReset();

        // Ensure the loader is stopped
        onStopLoading();

        if (mCursor != null && !mCursor.isClosed()) {
            mCursor.close();
        }
        mCursor = null;
    }
}

它只需要 AsyncTaskLoader 类.Android 3.0或更高版本的,或者兼容包自带的.

It only needs the AsyncTaskLoader class. Either the one in Android 3.0 or higher, or the one that comes with the compatibility package.

我也编写了一个与 LoadManager 兼容的 ListLoader,用于检索通用的 java.util.List 集合.

I also wrote a ListLoader which is compatible with the LoadManager and is used to retrieve a generic java.util.List collection.

这篇关于没有 ContentProvider 的 CursorLoader 使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-08 19:45