The use of Android:urimatcher

Source: Internet
Author: User

ContentProvider is one of the four major Android components, there are many articles on the Internet, basic usage can be found, but about urimatcher in the role of the article, there are examples, but I think it has not been clear.

First of all, why use Urimatcher.

ContentProvider provides a standard, and unique interface for querying, to the outside world:

[Java]View PlainCopyprint?
    1. Public final Cursor query (Uri uri, string[] projection,
    2. String selection, string[] Selectionargs, string sortOrder);



Where the URI is used to specify which data source, when a data source contains multiple content (such as multiple tables), it needs to be differentiated by different URIs, for example:

[Java]View PlainCopyprint?
    1. Public static final Uri content_uri_a = Uri.parse ("content://" + authority + "/" + table_a);
    2. Public static final Uri Content_uri_b = Uri.parse ("content://" + authority + "/" + Table_b);


At this time use Urimatcher can help us to filter to TableA or TableB, and then proceed to the next query, if not Urimatcher also can, we need to manually filter strings, with a bit of trouble, maintainability is not good.

And how to use Urimatcher, defined as follows:

[Java]View PlainCopyprint?
  1. Set up our URLs matchers to help us determine
  2. Incoming URI parameter is.
  3. Private static final Urimatcher Uri_matcher;
  4. static {
  5. Uri_matcher = new Urimatcher (Urimatcher.no_match);
  6. Uri_matcher.adduri (authority, table_a, table_a_msg);
  7. Uri_matcher.adduri (authority, Table_b, table_b_msg);
  8. }

To use Urimatcher in a query:

[Java]View PlainCopyprint?
  1. @Override
  2. Public Cursor query (Uri uri, string[] projection, String selection,
  3. String[] Selectionargs, String sortOrder) {
  4. String table = null;
  5. switch (Uri_matcher.match (URI)) {
  6. Case All_messages:
  7. Break ;
  8. Case oxford_msg:
  9. Table = table_a;
  10. Break ;
  11. Case chenyu_msg:
  12. Table = Table_b;
  13. Break ;
  14. Default:
  15. Break ;
  16. }
  17. Cursor resultcursor = mdb.query (table, projection, selection, Selectionargs, null, null, sortOrder);
  18. return resultcursor;
  19. }


In short, urimatcher is essentially a text filter, used in ContentProvider to help us filter and tell which data table the query wants to query.

The use of Android:urimatcher

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.