我是不是误会了什么?我正在尝试在 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/