SimpleCursorAdapter的旧构造函数已被弃用.. 真的吗?

9

这里SimpleCursorAdapter的API级别1构造函数已经被弃用,推荐使用LoaderManagerCursorLoader

但是深入研究LoaderManagerCursorLoader的使用后,我发现这个例子中,在扩展了ListFragment的内部类中创建了一个CursorLoader(我想它本身就是Fragment的扩展)。一切似乎都很正常,除了CursorLoader需要一个Uri作为参数。这意味着我需要创建一个ContentProvider来访问我的数据库。

我必须承认,为了创建一个简单的从数据库中获取项目的ListView,需要经历这么多步骤似乎有些过度。特别是如果我没有打算让我的数据库数据可供其他应用程序使用,而内容提供程序的主要目的就是这样做。
那真的值得吗?
特别是在像我这样的情况下,要获取的内容可能会很少。我正在认真考虑以传统方式完成它,你觉得呢?

1
你只支持API 11或更高版本吗? - Cristian
当然不是,我愿意使用兼容库来支持早期版本的Fragments和Loaders。 - Bilthon
你找到的示例叫什么名字(看起来像是我想在我的应用程序中做的事情)?链接只是一般解释示例。 - Karl
5个回答

8

我写了一个简单的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 或更高版本中提供的该类,也可以使用兼容包中提供的该类。

找到了一个不错的SimpleCursorLoader代码示例 - https://bitbucket.org/ssutee/418496_mobileapp/src/fc5ee705a2fd/demo/DotDotListDB/src/th/ac/ku/android/sutee/dotdotlist - 觉得非常有用! - Shushu

4
只需使用下面的构造函数,即带有标志的构造函数。不要使用FLAG_AUTO_REQUERY,只需将标志设为0。
除非您真的需要在用户查看ListView时处理底层数据库的数据更改,否则您不需要担心需要重新查询。
如果您想让ListView在用户查看列表时显示对数据库的更改,那么请遵循Google的建议并使用CursorLoader。
编辑:由于第二个构造函数仅在API 11中可用,因此您可能只想自己扩展CursorAdapter。您几乎只需要实现bindView和newView就可以了。

1

只使用simpleCursorAdapter过时的构造函数。在开发我的应用程序时出现了这种错误,但我使用它并且它与我的应用程序完美地配合。或者尝试使用Android开发人员网站下面的过时构造函数,它带有一个额外的参数即标志参数。


但请注意,第二个构造函数仅在API级别11中引入。因此,我必须添加兼容性库才能使用它。添加静态库也会使我的apk最终变得更大,如果只是为了使用那个构造函数,老实说我不认为这有任何意义。 - Bilthon

1

我相信CursorLoader目前是用于与ContentProvider一起使用的。

如果您希望直接从数据库中使用新框架加载,可以考虑扩展AsyncTaskLoader并从onCreateLoader返回它,而不是使用CursorLoader。

如果您正在使用现有方法,则必须更加注意查询操作需要多长时间。如果查询需要较长时间,请考虑使用AsyncTask来加载游标(并注意重新查询在UI线程中运行)。


0

我知道这个帖子有点老了,但你可以在创建SimpleCursorAdapter对象时添加一个最后的参数。只需添加“,0”即可。

这是Android喜欢的标志,警告就会消失。

Example:

SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.item_list_layout, cursor, fromDB(), toLayout(), 0);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接