没有ContentProvider的CursorLoader用法?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (28)

Android SDK文档说这种startManagingCursor()方法是有说服力的:

此方法已弃用。改用LoaderManager使用新的CursorLoader类; 这也可以通过Android兼容性套件在较早的平台上使用。此方法允许活动根据活动的生命周期来管理给定游标的生命周期。也就是说,当活动停止时,它会自动调用给定游标上的deactivate(),当它稍后重新启动时,它会为你调用requery()。当活动被破坏时,所有托管游标将自动关闭。如果您的目标是HONEYCOMB或更高版本,请考虑改为使用LoaderManager,而不是通过getLoaderManager()

所以我想用CursorLoader。但是我怎样才能使用它与自定义CursorAdapter和没有ContentProvider,当我需要构造函数中的URI CursorLoader

提问于
用户回答回答于

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或更高版本,还是兼容性软件包附带的版本。

用户回答回答于

编写你自己的装载器,它使用你的数据库类而不是内容提供者。最简单的方法就是CursorLoader从兼容库中获取类的源代码,并将提供者查询替换为你自己的db助手类的查询。

扫码关注云+社区