2011-06-03 74 views
9

在实施ContentProvider时定义所有uris的文档中有独特的建议。但我很困惑与URI匹配的部分:比如,我有包​​,表命名为“项”,然后我定义内容提供商URI匹配器

public static final Uri CONTENT_URI = 
    Uri.parse("content://org.company.example.sampleprovider/items"); 

我应该使用什么授权部分匹配静态初始化的URI:

private static final UriMatcher uriMatcher; 

    static { 
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 
    uriMatcher.addURI("what goes here?", "items", ITEM); 
    uriMatcher.addURI("what goes here?", "items/#", ITEM_ID); 
    } 

回答

14
public static final String PROVIDER_NAME = "org.company.example.sampleprovider"; 
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME); 
private static final UriMatcher uriMatcher; 
static { 
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 
    uriMatcher.addURI(PROVIDER_NAME, "items", ITEM); 
    uriMatcher.addURI(PROVIDER_NAME, "items/#", ITEM_ID); 
} 
+0

不应该我CONTENT_URI包含路径表? – 2011-06-03 11:39:32

+1

你不必,但它是可能的。它让你决定。我的内容提供者已经被命名为“BooksProvider”,所以我不需要添加额外的“/书籍”以向用户明确提供者的功能。 – 2011-06-03 11:43:17