Android ContentProvider URI方案,用于通知CursorAdapters监听OUTER JOIN查询

时间:2012-04-20 08:49:57

标签: android sqlite join cursor android-contentprovider

我有一个Android ContentProvider,可以对SQLite数据库进行LEFT OUTER JOIN次查询。

我们假设在数据库中我有3个表,UsersArticlesCommentsContentProvider类似于以下内容:

public class SampleContentProvider extends ContentProvider {
    private static final UriMatcher sUriMatcher;
    public static final String AUTHORITY = "com.sample.contentprovider";
    private static final int USERS_TABLE = 1;
    private static final int USERS_TABLE_ID = 2;
    private static final int ARTICLES_TABLE = 3;
    private static final int ARTICLES_TABLE_ID = 4;
    private static final int COMMENTS_TABLE = 5;
    private static final int COMMENTS_TABLE_ID = 6;
    private static final int ARTICLES_USERS_JOIN_TABLE = 7;
    private static final int COMMENTS_USERS_JOIN_TABLE = 8;

    // [...] other ContentProvider methods

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        String table = getTableName(uri);

        // SQLiteWrapper is a wrapper class to manage a SQLiteHelper
        Cursor c = SQLiteWrapper.get(getContext()).getHelper().getReadableDatabase()
                .query(table, projection, selection, selectionArgs, null, null, sortOrder);

        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        String table = getTableName(uri);

        // SQLiteWrapper is a wrapper class to manage a SQLiteHelper
        long id = SQLiteWrapper.get(getContext()).getHelper().getWritableDatabase()
                .insert(table, null, values);

        Uri itemUri = ContentUris.withAppendedId(uri, id);
        getContext().getContentResolver().notifyChange(itemUri, null);

        return itemUri;
    }

    private String getTableName(Uri uri) {
        switch (sUriMatcher.match(uri)) {
        case USERS_TABLE:
        case USERS_TABLE_ID:
            return "Users";

        case ARTICLES_TABLE:
        case ARTICLES_TABLE_ID:
            return "Articles";

        case COMMENTS_TABLE:
        case COMMENTS_TABLE_ID:
            return "Comments";

        case ARTICLES_USERS_JOIN_TABLE:
            return "Articles a LEFT OUTER JOIN Users u ON (u._id = a.user_id)";

        case COMMENTS_USERS_JOIN_TABLE:
            return "Comments c LEFT OUTER JOIN Users u ON (u._id = c.user_id)";

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(AUTHORITY, "users", USERS_TABLE);
        sUriMatcher.addURI(AUTHORITY, "articles", ARTICLES_TABLE);
        sUriMatcher.addURI(AUTHORITY, "comments", COMMENTS_TABLE);
        sUriMatcher.addURI(AUTHORITY, "users" + "/#", USERS_TABLE_ID);
        sUriMatcher.addURI(AUTHORITY, "articles" + "/#", ARTICLES_TABLE_ID);
        sUriMatcher.addURI(AUTHORITY, "comments" + "/#", COMMENTS_TABLE_ID);
        sUriMatcher.addURI(AUTHORITY, "???", ARTICLES_USERS_JOIN_TABLE); // what uri here?
        sUriMatcher.addURI(AUTHORITY, "???", COMMENTS_USERS_JOIN_TABLE); // what uri here?
    }
}

每次在CursorAdapter表中插入(或更新)行时,通知所有Users监听已加入和未加入的查询的最佳URI方案是什么?

换句话说,如果我在其中一个表格中添加或更新新行,我希望使用getContext().getContentResolver().notifyChange(itemUri, null)发送单个通知,以便所有CursorAdapter收听任何查询(USERS_TABLEARTICLES_USERS_JOIN_TABLECOMMENTS_USERS_JOIN_TABLE)会收到更新其内容的通知。

如果无法做到这一点,是否有其他方式通知所有观察员?

2 个答案:

答案 0 :(得分:5)

您可以使用特殊的Uri来查询:

    sUriMatcher.addURI(AUTHORITY, "articlesusers", ARTICLES_USERS_JOIN_TABLE);
    sUriMatcher.addURI(AUTHORITY, "commentsusers", COMMENTS_USERS_JOIN_TABLE);

但我想不出发送单一通知的方法。看来你最好的选择是为每个引用被修改表的Uri发送通知。因此,您的insert / update / delete方法会多次调用notifyChange,具体取决于受影响的表。对于“用户”的更改,它将是3个通知 - 用户,文章用户和评论用户 - 因为它们都依赖于“用户”表。

答案 1 :(得分:1)

正如prodaea所述,这是您可以用于通知Uri的另一种选择。这不是一个完美的解决方案,但它只使用一个Uri进行通知。

解决方案是使用主要的Uri而不使用任何表名(例如:content://com.example.app.provider/)作为ARTICLES_USERS_JOIN_TABLE和{{1}的查询方法中的通知Uri }。因此,只要任何表发生更改,就会通知相关游标。但有一个限制。也就是说,即使COMMENTS_USERS_JOIN_TABLE表中有变化,也会通知ARTICLES_USERS_JOIN_TABLE光标。

对于表格Articles文章',您可以使用其特定的Uris进行通知。