gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:20:17 25 4
gpt4 key购买 nike

我有一个 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?
}
}

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

换句话说,如果我在其中一个表中添加或更新新行,我想用 getContext().getContentResolver().notifyChange(itemUri , null) 以便所有 CursorAdapter 监听任何查询>) 收到更新其内容的通知。

如果这不可能,是否有其他方法通知所有观察者?

最佳答案

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

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

但我想不出发送单个通知的方法。看来您最好的选择是为每个引用正在修改的表的 Uri 发送通知。因此,您的插入/更新/删除方法将根据受影响的表多次调用 notifyChange。对于“用户”的更改,它将是 3 个通知——用户、文章用户和评论用户——因为它们都依赖于“用户”表。

关于Android ContentProvider URI 方案通知 CursorAdapters 监听 OUTER JOIN 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10243017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com