Android. content. urimatcher

Source: Internet
Author: User
The document has been well understood: 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 treeUrimatcherObjects. 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 lele_phones_id = 4; Private Static final int people_contactmethods = 7; private Static final int lele_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 CALS = 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/#", lele_phones_id); surimatcher. adduri ("contacts", "People/#/contact_methods", lele_contactmethods); surimatcher. adduri ("contacts", "People/#/contact_methods/#", lele_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", "CILS", CILS); surimatcher. adduri ("call_log", "CILS/filter/*", calls_filter); surimatcher. adduri ("call_log", "CILS/#", calls_id );}

Then when you need to match against a URI, callMatch (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 wowould 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/snil-mail "; Case people_address_id: Return" Vnd. android. cursor. item/snil-mail "; default: return NULL ;}}

Instead:

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/snil-mail ";} else if (pathsegments. size () = 3) {return "Vnd. android. cursor. item/snil-mail ";}} return NULL ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.