UriMatcher交回错误的价值

时间:2010-09-15 15:52:27

标签: android uri android-contentprovider

我有两个Uris。假设它们是:

content://myprovider/messages
content://myprovider/messages/#

在我的扩展ContentProvider中,我声明了以下内容:

    private static final int MESSAGES = 1;
    private static final int MESSAGES_ID = 2;

    private static final UriMatcher sUriMatcher;

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI("myprovider", "messages", MESSAGES);
        sUriMatcher.addURI("myprovider", "messages/#", MESSAGES_ID);
    }

当我使用...

content://myprovider/messages/#

... Uri在调用任何ContentProvider方法时,比如说insert,而我的ContentProvider方法会调用...

sUriMatcher.match(uri)

...我回来1(MESSAGES)。

为什么???

1 个答案:

答案 0 :(得分:1)

如果您想获得MESSAGES_ID,则必须像content://myprovider/messages/12345一样编写uri,或者您必须为MESSAGES_ID添加模式,如:

sUriMatcher.addURI("myprovider", "messages/*", MESSAGES_ID);