不使用 ContentProvider 时使用 CursorLoader

2024-02-11

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

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

所以我想用CursorLoader。但我如何将它与自定义一起使用CursorAdapter并且没有ContentProvider,当我在构造函数中需要 URI 时CursorLoader?


我写了一个简单的CursorLoader https://gist.github.com/1217628不需要内容提供商:

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以上版本,要么是兼容包自带的。

I also wrote a ListLoader https://github.com/casidiablo/groundy/blob/d60a51760582b8a03ea9a06ea7ca938d3785ea15/library/src/main/java/com/telly/groundy/loader/ListLoader.java这是兼容的LoadManager并用于检索通用的java.util.List收藏。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不使用 ContentProvider 时使用 CursorLoader 的相关文章