没有ContentProvider的CursorLoader用法

sea*_*kej 107 android cursor android-contentprovider android-loadermanager android-cursorloader

Android SDK文档说该startManagingCursor()方法已被删除:

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

所以我想用CursorLoader.但是当我在构造函数中需要URI时,如何使用自定义CursorAdapter和不使用它?ContentProviderCursorLoader

Cri*_*ian 154

我写了一个不需要内容提供者的简单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;
    }
}
Run Code Online (Sandbox Code Playgroud)

它只需要AsyncTaskLoader上课.Android 3.0或更高版本中的一个,或兼容包附带的那个.

我还写了一个ListLoader与之兼容的LoadManager并且用于检索泛型java.util.List集合.

  • 好东西!用户应该意识到一个限制,即它没有机制来刷新数据更改(因为Loaders应该这样做) (14认同)
  • 找到一个很好的代码示例使用它 - https://bitbucket.org/ssutee/418496_mobileapp/src/fc5ee705a2fd/demo/DotDotListDB/src/th/ac/ku/android/sutee/dotdotlist - 发现它非常有用! (13认同)
  • 许可证是Apache 2.0; 您可以随时随地重复使用它.如果您有任何改进,请告诉我. (2认同)

Nik*_*kov 23

编写自己的使用数据库类而不是内容提供者的加载器.最简单的方法是CursorLoader从兼容性库中获取类的源代码,并将提供者查询替换为对您自己的db帮助程序类的查询.


emm*_*mby 14

SimpleCursorLoader是一个简单的解决方案,但它不支持在数据更改时更新加载器.CommonsWare有一个loaderex库,可以添加SQLiteCursorLoader并支持对数据更改进行重新查询.

https://github.com/commonsguy/cwac-loaderex

  • 但是,要使用自动重新查询,您需要为UI和更新使用相同的加载程序,从而限制其对后台服务的可用性. (2认同)

Tim*_*Ohr 12

第三种选择是简单地覆盖loadInBackground:

public class CustomCursorLoader extends CursorLoader {
    private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver();

    @Override
    public Cursor loadInBackground() {
        Cursor cursor = ... // get your cursor from wherever you like

        if (cursor != null) {
            // Ensure the cursor window is filled
            cursor.getCount();
            cursor.registerContentObserver(mObserver);
        }

        return cursor;
    }
};
Run Code Online (Sandbox Code Playgroud)

这还将在数据库更改时重新查询光标.

唯一需要注意的是:你必须定义另一个观察者,因为Google的无限智慧决定让他们的包装变得私密.如果您将类放入与原始类(或compat)相同的包中,您实际上可以使用原始观察者.观察者是一个非常轻量级的对象,并没有在其他任何地方使用,所以这并没有太大的区别.

  • 是啊.在你的CustomLoader的`loadInBackground()`中,在返回光标之前,说`cursor.setNotificationUri(getContext().getContentResolver(),uri);'uri可能只是来自随机字符串,如`Uri.parse("content:// query_slot1" )`.好像它不关心uri真的存在与否.一旦我在DB上完成操作.说`getContentResolver().notifyChange(uri,null);`会做的伎俩.然后我可以在少量查询的应用程序文件中创建一些"查询uri插槽".我测试在运行时插入DB记录,它似乎工作,但我仍然怀疑这是好的做法.有什么建议吗? (4认同)