android.content.UriMatcher

來源:互聯網
上載者:User
文檔已經講得好明白了:http://developer.android.com/reference/android/content/UriMatcher.html

Utility class to aid in matching URIs in content providers.

To use this class, build up a tree of UriMatcher objects. For example:

    private static final int PEOPLE = 1;    private static final int PEOPLE_ID = 2;    private static final int PEOPLE_PHONES = 3;    private static final int PEOPLE_PHONES_ID = 4;    private static final int PEOPLE_CONTACTMETHODS = 7;    private static final int PEOPLE_CONTACTMETHODS_ID = 8;    private static final int DELETED_PEOPLE = 20;    private static final int PHONES = 9;    private static final int PHONES_ID = 10;    private static final int PHONES_FILTER = 14;    private static final int CONTACTMETHODS = 18;    private static final int CONTACTMETHODS_ID = 19;    private static final int CALLS = 11;    private static final int CALLS_ID = 12;    private static final int CALLS_FILTER = 15;    private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);    static    {        sURIMatcher.addURI("contacts", "people", PEOPLE);        sURIMatcher.addURI("contacts", "people/#", PEOPLE_ID);        sURIMatcher.addURI("contacts", "people/#/phones", PEOPLE_PHONES);        sURIMatcher.addURI("contacts", "people/#/phones/#", PEOPLE_PHONES_ID);        sURIMatcher.addURI("contacts", "people/#/contact_methods", PEOPLE_CONTACTMETHODS);        sURIMatcher.addURI("contacts", "people/#/contact_methods/#", PEOPLE_CONTACTMETHODS_ID);        sURIMatcher.addURI("contacts", "deleted_people", DELETED_PEOPLE);        sURIMatcher.addURI("contacts", "phones", PHONES);        sURIMatcher.addURI("contacts", "phones/filter/*", PHONES_FILTER);        sURIMatcher.addURI("contacts", "phones/#", PHONES_ID);        sURIMatcher.addURI("contacts", "contact_methods", CONTACTMETHODS);        sURIMatcher.addURI("contacts", "contact_methods/#", CONTACTMETHODS_ID);        sURIMatcher.addURI("call_log", "calls", CALLS);        sURIMatcher.addURI("call_log", "calls/filter/*", CALLS_FILTER);        sURIMatcher.addURI("call_log", "calls/#", CALLS_ID);    }

Then when you need to match against a URI, call match(Uri), providing the URL that you have been given. You can use the result to build a query, return a type, insert or delete a row, or whatever you need, without duplicating all of the if-else logic that you would otherwise need. For example:

    public String getType(Uri url)    {        int match = sURIMatcher.match(url);        switch (match)        {            case PEOPLE:                return "vnd.android.cursor.dir/person";            case PEOPLE_ID:                return "vnd.android.cursor.item/person";... snip ...                return "vnd.android.cursor.dir/snail-mail";            case PEOPLE_ADDRESS_ID:                return "vnd.android.cursor.item/snail-mail";            default:                return null;        }    }

instead of:

    public String getType(Uri url)    {        List pathSegments = url.getPathSegments();        if (pathSegments.size() >= 2) {            if ("people".equals(pathSegments.get(1))) {                if (pathSegments.size() == 2) {                    return "vnd.android.cursor.dir/person";                } else if (pathSegments.size() == 3) {                    return "vnd.android.cursor.item/person";... snip ...                    return "vnd.android.cursor.dir/snail-mail";                } else if (pathSegments.size() == 3) {                    return "vnd.android.cursor.item/snail-mail";                }            }        }        return null;    }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.