没有ContentProvider的CursorLoader用法

时间:2011-08-24 21:26:28

标签: android cursor android-cursorloader android-loadermanager android-contentprovider

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

  

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

所以我想使用CursorLoader。但是,当我在CursorAdapter的构造函数中需要URI时,如何将其与自定义ContentProviderCursorLoader一起使用?

5 个答案:

答案 0 :(得分:154)

我写了一篇不需要内容提供商的simple 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或更高版本中的一个,或兼容包附带的那个。

我还wrote a ListLoaderLoadManager兼容,用于检索通用java.util.List集合。

答案 1 :(得分:23)

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

答案 2 :(得分:14)

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

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

答案 3 :(得分: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;
    }
};

这也将在数据库更改时重新查询光标。

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

答案 4 :(得分:2)

Timo Ohr提出的第三个选项以及Yeung的评论提供了最简单的答案(奥卡姆剃刀)。下面是一个适合我的完整课程的示例。使用此类有两个规则。

  1. 扩展此抽象类并实现方法getCursor()和getContentUri()。
  2. 任何时候基础数据库发生变化(例如,插入或删除后),请务必调用

    ?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      <LinearLayout
            android:id="@+id/linear_device"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal"
            >
          <Button
              android:text="Add Device"
              android:layout_width="206.0dp"
              android:layout_height="43.3dp"
              android:id="@+id/button_device_vazhdo"
              android:layout_gravity="center"
              android:layout_marginStart="10dp"
              android:layout_marginRight="10dp"
              android:layout_marginBottom="10dp"
              android:background="@android:color/holo_green_dark" />
        </LinearLayout>
      <LinearLayout 
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview_ping"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        />
      </LinearLayout>
    </LinearLayout>
    

    其中myUri与您的方法getContentUri()实现返回的相同。

  3. 以下是我使用的类的代码:

    getContentResolver().notifyChange(myUri, null);