3

複数のテーブルがあり、次のような URI を使用する SQLite データベース用のコンテンツ プロバイダーがあります。

Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);

これは、すべての行に 1 つの CONTENT_TYPE と 1 つの行に 1 つの URI を使用して、1 つのデータベース テーブルに 1 つの URI を使用する標準的なパターンのようです。

ただし、テーブル データのサブセットの URI が必要です。現在、データベースに大量のテーブルを追加することは意味がありません。コンテンツ プロバイダーはこれを処理するように設計されているようですが、私にはわかりません。基本的に、テーブルではなくクエリを指す URI が必要です。それが理にかなっていることを願っています。

4

1 に答える 1

4

アプリケーションで新しい URI が必要な場所に追加して、コンテンツ プロバイダーのクエリ メソッドを変更するだけです。

public class ExampleProvider extends ContentProvider {

    private static final UriMatcher sUriMatcher;


    sUriMatcher.addURI("com.example.app.provider", "table3", 1);
    sUriMatcher.addURI("com.example.app.provider", "table3/#", 2);
    sUriMatcher.addURI("com.example.app.provider", "table3/customquery", 3);

public Cursor query(
    Uri uri,
    String[] projection,
    String selection,
    String[] selectionArgs,
    String sortOrder) {

    switch (sUriMatcher.match(uri)) {


        // If the incoming URI was for all of table3
        case 1:

            if (TextUtils.isEmpty(sortOrder)) sortOrder = "_ID ASC";
            break;

        // If the incoming URI was for a single row
        case 2:

            /*
             * Because this URI was for a single row, the _ID value part is
             * present. Get the last path segment from the URI; this is the _ID value.
             * Then, append the value to the WHERE clause for the query
             */
            selection = selection + "_ID = " uri.getLastPathSegment();
            break;
        case 3:
             // handle your custom query here

             break;

    }
    // call the code to actually do the query
}
于 2012-05-12T22:47:06.347 に答える