1

I have followed this article for creating a custom content provider http://xjaphx.wordpress.com/2011/06/19/create-and-use-custom-content-provider/. The problem I'm facing is with the URI and URIMatcher. I know we can urimatcher to match uri for a single item vs uri for list of items. But what If i dont use any uri matching concept eg I can write

CONTENT_URI is "pete.android.contentprovider"
String where = BaseColumns.COLUMN_ID + "?"
getContentResolver.delete(CONTENT_URI, where, new String[]{"2"})

The above query will delete the 2 row from the database table. If I can do this directly by using the where clause then why should I use switch case in delete function to determine whether the uri is for single item or multiple item.

What is the standard approach. I'm totally confused with this and cannot find a proper answer on any blog or forum.

4

2 回答 2

4

对于一件事,您确实希望将所有数据逻辑从应用程序逻辑中抽象出来并放入 contentProvider。这只是一个好习惯。如果您的数据架构发生变化怎么办?

尝试通过附加 ID 来构建特定于 id 的 URI 字符串,如下所示:

Uri singleUri = ContentUri.withAppendedId(CONTENT_URI, 2); 

然后使用 URI Matcher 开关案例的特定分支构建查询。

于 2013-10-08T10:08:25.453 回答
0

是的,您必须对 Item 类型和 Dir 类型(许多项目)有单独的逻辑

在项目类型下,您可以直接删除项目。因为您确切地知道您的项目。在 dir 类型下,您可以对需要过滤需要删除的项目的逻辑使用 where 子句。

于 2013-10-08T10:09:05.403 回答