获得行uri后,从内容提供商处获取数据的正确方法是什么?

时间:2012-10-26 12:33:22

标签: android android-contentprovider

我想知道在我已经知道行uri的情况下是否有可能直接在游标(或其他容器)中获取内容提供者行中存在的数据(换句话说,我已经拥有了附加了行ID的ContentURI。

插入或内容观察器OnChange(在android api 16上)都会返回插入/更改的URI,我希望有效地从该行获取数据(没有很多查询或指令)。但是,我发现的唯一方法是从URI解析ID,使用该id的selection子句构建一个查询,然后获取光标,如下所示:

     long row = ContentUris.parseId(uri);
     String mSelectionClause = ContentProvider.Table._ID + " = ?";
     String[] mSelectionArgs = {Long.toString(row)};
     Uri other_uri = Uri.parse(ContentProvider.AUTHORITY_STRING + ContentProvider.UriPathIndex.Table);
     Cursor cursor = cr.query(other_uri ,null,mSelectionClause,mSelectionArgs,null);

还有其他方法吗?我想知道是否有可能直接使用行uri

1 个答案:

答案 0 :(得分:5)

引自http://developer.android.com/guide/topics/providers/content-provider-basics.html#ContentURIs

许多提供程序允许您通过在URI的末尾附加ID值来访问表中的单个行。例如,要从用户词典中检索_ID为4的行,可以使用此内容URI:

Uri singleUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI,4);

编辑(来自评论): 它适用于所有标准化内容提供商(至少由Google提供)。它适用于所有内容提供商,这些提供商遵循基于行的数据存储备份的内容提供商的设计准则。