SimpleCursorAdapter的老构造德precated ..真的吗? [英] SimpleCursorAdapter's old constructor deprecated.. really?

查看:122
本文介绍了SimpleCursorAdapter的老构造德precated ..真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href="http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#SimpleCursorAdapter%28android.content.Context,%20int,%20android.database.Cursor,%20java.lang.String%5B%5D,%20int%5B%5D%29">Here它说, SimpleCursorAdapter 的API级别1构造德precated和使用 LoaderManager CursorLoader 推荐。

Here it says that SimpleCursorAdapter's API level 1 constructor is deprecated and the use of LoaderManager and CursorLoader is recommended.

不过,潜心钻研 LoaderManager CursorLoader 的使用,我发现<一href="http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/LoaderCursorSupport.html">this例子是扩展了 ListFragment (片段本身的延伸,我想),我们创建了一个 CursorLoader 内部类中。一切似乎确定,除了一个事实,即 CursorLoader 需要乌里作为参数。因此,这意味着我需要创建一个的ContentProvider 以访问我的数据库。

But delving into the LoaderManager and CursorLoader's use I found this example where inside an inner class that extends a ListFragment (an extension of Fragment itself I suppose) we create a CursorLoader. Everything seems ok, except for the fact that CursorLoader takes a Uri as an argument. So this implies that I need to create a ContentProvider to get access to my database.

我必须承认,它看起来像一个矫枉过正必须经历这一切只是为了创建一个简单的的ListView 与项目从数据库的到来。特别是如果我不打算让我提供给其他应用程序的数据库的数据和内容提供商的主要目的是为了做到这一点。

I must confess it looks like an overkill to have to go through all of this just to create a simple ListView with items coming from a database. Specially if I have no intention of making my database data available to other apps, and the main purpose of a content provider is to do that.

那么,这真的值得吗?

So is it really worth it?

特别是像我所在的内容要取出的情况下,很可能将是小的。我认真考​​虑做旧的方式,你看怎么样?

Especially in cases like mine where the content to be fetched is likely going to be small. I'm seriously considering doing it the old way, what do you say?

推荐答案

我写href="https://gist.github.com/1217628" rel="nofollow">简单CursorLoader 一个

I wrote a simple CursorLoader that does not need a content provider:

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 类。无论是一个在安卓3.0或更高版本,或附带的兼容包的人。

It only needs the AsyncTaskLoader class. Either the one in Android 3.0 or higher, or the one that comes with the compatibility package.

这篇关于SimpleCursorAdapter的老构造德precated ..真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆