9

这里它说SimpleCursorAdapter's API level 1 构造函数已被弃用,建议使用LoaderManagerand CursorLoader

但是深入研究LoaderManagerandCursorLoader的使用我发现了这个例子,在一个扩展 a 的内部类中ListFragment(我想是 Fragment 本身的扩展)我们创建了一个CursorLoader. CursorLoader一切似乎都很好,除了将 aUri作为参数的事实。所以这意味着我需要创建一个ContentProvider来访问我的数据库。

ListView我必须承认,为了使用来自数据库的项目创建一个简单的项目,必须经历所有这些看起来有点过头了。特别是如果我不打算将我的数据库数据提供给其他应用程序,而内容提供者的主要目的就是这样做。

那么它真的值得吗?

特别是在像我这样要获取的内容可能很小的情况下。我正在认真考虑用旧方法做,你说呢?

4

5 回答 5

8

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

于 2011-09-14T20:08:39.817 回答
4

只需使用它下面的构造函数,即接受标志的构造函数。不要使用 FLAG_AUTO_REQUERY,只需为标志传递 0。

除非您确实需要在用户查看 ListView 时处理对底层数据库的数据更改,否则您无需担心需要重新查询。

另一方面,如果您希望 ListView 在用户查看列表时显示对数据库的更改,那么请遵循 Google 的建议并使用 CursorLoader。

编辑:

由于第二个构造函数仅在 API 11 中可用,您可能只想自己扩展 CursorAdapter。你几乎只需要实现 bindView 和 newView 就完成了。

于 2011-08-31T04:42:39.937 回答
1

Use simpleCursorAdapter deprecated constructor only. This kind of error appeared when I was developing my app but i used it and it worked perfectly with my app. Or try to use the constructor below deprecated one in android developers website which has an extra argument i.e the flag argument with it.

于 2011-08-31T05:28:09.887 回答
1

I believe CursorLoader is currently intended for use with a ContentProvider.

If you wish to load directly from your database using the new framework; you can consider extending AsyncTaskLoader and returning it from onCreateLoader instead of using a CursorLoader.

If you are using the existing methods you have to be more careful of how long your query operation will take. If your query will take noticable amounts of time consider using an AsyncTask to load the cursor (and be aware of requery running in the UI thread).

于 2011-08-31T05:31:50.350 回答
0

我知道这个线程很旧,但您可以在 SimpleCursorAdapter 对象创建中添加最后一个参数。只需添加“,0”。

这是 Android 喜欢的标志,并且警告消失了。

例子:

SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.item_list_layout, cursor, fromDB(), toLayout(), 0);
于 2014-06-11T23:49:09.283 回答