Android 内容提供商相同的 URI 不匹配

标签 android uri android-contentprovider

我是不是误会了什么?我正在尝试在 Android 中实现 ContentProvider,但由于某种原因调用 URI 不匹配。 在我的 ContentProvider 中,我定义了以下内容:

private static final int GET_COURSES = 100;
public static final Uri COURSES_URI = Uri.withAppendedPath(CONTENT_URI, CourseTable.NAME);
private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
static 
{
    matcher.addURI(AUTHORITY, COURSES_URI.toString(), GET_COURSES);
}

然后,在我的查询调用中:

public Cursor query(Uri uri, ...)
{
    int type = matcher.match(uri);
    .
    .

在这里,type 总是 -1...在调试窗口中,我查看了传入的 uri 和 COURSES_URI,并且字符串表示是相同的...

有什么建议吗?

谢谢

更新:

我调用 Content Provider 使用:

new CursorLoader(this, CoursesProvider.COURSES_URI, null, null, null, null);

...这让我感到困惑...刚刚得到 uri.equals(COURSES_URI) == true,所以 UriMatcher 中一定有什么地方不正确

最佳答案

问题解决了... 最初的问题是 COURSES_URI 还包含 AUTHORITY 路径:

private static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + DBManager.DB_NAME);
private static final Uri COURSES_URI = Uri.withAppendedPath(CONTENT_URI, CourseTable.NAME);

matcher.AddURI(authority,path,code) 方法中,应该删除路径的授权部分。

这可以使用 COURSES_URI.getPath().substring(1) 获得(删除 getPath() 返回的前导 '/' 的子字符串)

关于Android 内容提供商相同的 URI 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12024315/

相关文章:

android - 无法解析 com.android.tools.build :gradle:0. 5.+

java - 神秘导出apk

android - 如何更改 native 推送通知状态栏图标?

android - 在android中获取联系人照片的文件系统路径

android - 如何为一系列表行而不是整个表创建内容提供者 URI

android - ContentProvider 不会出现在 Data & Synchronization 区域

android - Eclipse - 单击 XML 内容会删除/重新排列它吗?

javascript - 如何在不刷新页面的情况下更改 URI?

android - 从 Intent 中获取音频uri

ios - 适用于 iOS 的 Google OAuth 2.0 没有返回到我的应用程序