2011-06-03 75 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