Android UriMatcher与通配符不匹配

时间:2015-05-25 21:51:10

标签: android regex android-contentprovider

我有ContentProvider,我需要将包含URIs的{​​{1}}作为通配符。

来自ContentProvider的

UriMatcher:

UUIDs

通知查询代码:

public static final Uri CONTENT_URI_NOTIFICATIONS = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH_NOTIFICATIONS);
public static final Uri CONTENT_URI_USERS  = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH_USERS);

private static final int NOTIFICATIONS = 40;
private static final int USER_ID = 70;

private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);

static
{
    sURIMatcher.addURI(AUTHORITY, BASE_PATH_NOTIFICATIONS, NOTIFICATIONS);
    sURIMatcher.addURI(AUTHORITY, BASE_PATH_USERS + "/*", USER_ID);
}

USER_ID的查询代码:

 Uri uri = Uri.parse(String.valueOf(MyContentProvider.CONTENT_URI_NOTIFICATIONS));
 return new CursorLoader(getActivity(), uri, projection, null, null, null);

从上面的UriMatcher中, NOTIFICATIONS URI匹配,但 USER_ID 不匹配。知道这里有什么不对吗?值得注意的是,当我过去使用整数而不是UUID来表示用户并且曾经在UriMatcher中使用#而不是*时,一切都按预期工作。切换到通配符后,匹配器停止匹配包含UUID的URI。

1 个答案:

答案 0 :(得分:2)

显然,添加URI的顺序很重要。如果您设置如下,USER_DETAILS将不再被识别。您必须先切换订单并先添加USER_DETAILS。

sURIMatcher.addURI(AUTHORITY, BASE_PATH_USERS + "/*", USER_ID);
sURIMatcher.addURI(AUTHORITY, BASE_PATH_USERS + "/details", USER_DETAILS);