9

ここでは、SimpleCursorAdapterのAPIレベル1コンストラクターは非推奨であり、およびの使用が推奨されていることを示していLoaderManagerますCursorLoader

LoaderManagerしかし、との使用法を掘り下げてみると、 (私が推測するフラグメント自体の拡張)を拡張する内部クラス内にを作成するこのCursorLoader例が見つかりました。引数としてaを取るという事実を除いて、すべてが大丈夫のようです。つまり、これは、データベースにアクセスするためにを作成する必要があることを意味します。ListFragmentCursorLoaderCursorLoaderUriContentProvider

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を見ているときに、基になるDBへのデータ変更を実際に処理する必要がない限り、再クエリの必要性について心配する必要はありません。

一方、ユーザーがリストを見ているときにListViewにDBへの変更を表示させたい場合は、Googleのアドバイスに従い、CursorLoaderを使用します。

編集:

2番目のコンストラクターはAPI11でのみ使用できるため、CursorAdapterを自分で拡張することもできます。bindViewとnewViewを実装するだけで、完了です。

于 2011-08-31T04:42:39.937 に答える
1

CursorLoaderは現在、ContentProviderでの使用を目的としていると思います。

新しいフレームワークを使用してデータベースから直接ロードする場合。CursorLoaderを使用する代わりに、AsyncTaskLoaderを拡張してonCreateLoaderから返すことを検討できます。

既存のメソッドを使用している場合は、クエリ操作にかかる時間にさらに注意する必要があります。クエリにかなりの時間がかかる場合は、AsyncTaskを使用してカーソルをロードすることを検討してください(UIスレッドで実行されている再クエリに注意してください)。

于 2011-08-31T05:31:50.350 に答える
1

simpleCursorAdapterの非推奨コンストラクターのみを使用してください。この種のエラーは、アプリの開発中に表示されましたが、使用したところ、アプリで完全に機能しました。または、Android開発者のWebサイトで非推奨のコンストラクターを使用してみてください。このコンストラクターには、追加の引数、つまりフラグ引数があります。

于 2011-08-31T05:28:09.887 に答える
0

このスレッドは古いことは知っていますが、SimpleCursorAdapterオブジェクトの作成に最後のパラメーターを追加するだけで済みます。「、0」を追加するだけです。

これはAndroidが好むフラグであり、警告は消えます。

例:

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