Urimatcher, Contenturist, and Contentresolver in Android

Source: Internet
Author: User

Because the URI represents the data to manipulate, we often need to parse the URI and fetch the data from the URI. The Android system provides two tool classes for manipulating URIs, Urimatcher and Contenturis, respectively. Mastering their use will facilitate our development efforts.

Urimatcher: Used to match the URI, it uses the following:
1. First put the URI path you need to match all to the registration, as follows:

Java code:


    1. The constant Urimatcher.no_match represents a return code (-1) that does not match any path.

    2. Urimatcher urimatcher = new Urimatcher (urimatcher.no_match);

    3. If the match () method matches the Content:.sqlite.provider.contactprovider/contact path, a match code of 1 is returned

    4. Urimatcher.adduri ("Com.changcheng.sqlite.provider.contactprovider", "Contact", 1);
    5. Add need to match URI, if match will return match code

    6. If the match () method matches the content.sqlite.provider.contactprovider/contact/230 path, a match code of 2 is returned

    7. Urimatcher.adduri ("Com.changcheng.sqlite.provider.contactprovider", "contact/#", 2);
    8. #号为通配符

Copy Code

2. After registering a URI that needs to be matched, you can use the Urimatcher.match (URI) method to match the input URI, and if the match returns a matching code, the match code is the third parameter passed in by calling the Adduri () method, assuming that the content:. Sqlite.provider.contactprovider/contact path, the matching code returned is 1.Contenturis:Used to get the ID part after the URI path, it has two more practical methods: Withappendedid (URI, id) is used to add the ID portion of the path to the Parseid (URI) method to get the ID part from the pathContentresolver:When external applications need to add, delete, modify, and query the data in ContentProvider, you can use the Contentresolver class to get the Contentresolver object, You can use the Getcontentresolver () method provided by the activity. Contentresolver uses the INSERT, delete, update, and query methods to manipulate the data.

ContentProvider Sample ProgramWe added a contentprovider for yesterday's SQLite sample program for other apps to access our data.1.Add the ContentProvider class for the SQLite sample program

Java code:

  1. Package eoe.sqlite.provider;

  2. Import Com.changcheng.sqlite.MyOpenHelper;
  3. Import Android.content.ContentProvider;
  4. Import Android.content.ContentUris;
  5. Import android.content.ContentValues;
  6. Import Android.content.UriMatcher;
  7. Import Android.database.Cursor;
  8. Import Android.database.sqlite.SQLiteDatabase;
  9. Import Android.net.Uri;

  10. public class Contactcontentprovider extends ContentProvider {
  11. Matching external requests via Urimatcher
  12. private static Urimatcher Urimatcher = new Urimatcher (urimatcher.no_match);
  13. Database read and write via Openhelper
  14. Private Myopenhelper Openhelper;
  15. Match State constants
  16. private static final int contact_list = 1;
  17. private static final int contact = 2;

  18. Table name
  19. private static final String TableName = "Contacts";
  20. Add URI
  21. static {
  22. Urimatcher.adduri ("Com.changcheng.sqlite.provider", "Contact", contact_list);
  23. Urimatcher.adduri ("Com.changcheng.sqlite.provider", "contact/#", contact);
  24. }


  25. @Override
  26. public int Delete (URI Uri, String selection, string[] Selectionargs) {
  27. Sqlitedatabase db = This.openHelper.getWritableDatabase ();
  28. int result;


  29. Switch (Urimatcher.match (URI)) {
  30. Case Contact_list:
  31. result = Db.delete (TableName, Selection, Selectionargs);
  32. Break
  33. Case CONTACT:
  34. Long id = Contenturis.parseid (URI);
  35. String where = "_id=" + ID;

  36. if (selection! = NULL &&! "". Equals (selection)) {
  37. where = where + "and" + selection;
  38. }

  39. result = Db.delete (TableName, where, Selectionargs);
  40. Break
  41. Default
  42. throw new IllegalArgumentException ("uri illegalargument:" + uri);
  43. }
  44. return result;
  45. }

  46. @Override
  47. Public String GetType (Uri uri) {
  48. Switch (Urimatcher.match (URI)) {
  49. Case contact_list://The collection type must precede the vnd.android.cursor.dir/
  50. return "Vnd.android.cursor.dir/contactlist";
  51. Case contact://The non-collection type must precede the vnd.android.cursor.item/
  52. return "Vnd.android.cursor.item/contact";
  53. Default
  54. throw new IllegalArgumentException ("uri illegalargument:" + uri);
  55. }
  56. }

  57. @Override
  58. Public URI insert (URI uri, contentvalues values) {
  59. Sqlitedatabase db = This.openHelper.getWritableDatabase ();
  60. Long ID;
  61. Switch (Urimatcher.match (URI)) {
  62. Case Contact_list:
  63. Because SQL statements need to be generated in the background, the second argument must be raised when values are null. The generated SQL statement will not go wrong!
  64. id = Db.insert (tableName, "_id", values);
  65. Return Contenturis.withappendedid (URI, id);
  66. Case CONTACT:
  67. id = Db.insert (tableName, "_id", values);
  68. String Uripath = uri.tostring ();
  69. String path = uripath.substring (0, Uripath.lastindexof ("/")) + ID;
  70. return Uri.parse (path);
  71. Default
  72. throw new IllegalArgumentException ("uri illegalargument:" + uri);
  73. }
  74. }


  75. @Override
  76. public Boolean onCreate () {
  77. This.openhelper = new Myopenhelper (This.getcontext ());
  78. return true;
  79. }

  80. @Override
  81. Public Cursor query (URI uri, string[] projection, String selection,
  82. String[] Selectionargs, String sortOrder) {
  83. Sqlitedatabase db = This.openHelper.getWritableDatabase ();
  84. Switch (Urimatcher.match (URI)) {
  85. Case Contact_list:
  86. Return Db.query (TableName, projection, selection, selectionargs,null, NULL, sortOrder);

  87. Case CONTACT:
  88. Long id = Contenturis.parseid (URI);
  89. String where = "_id=" + ID;
  90. if (selection! = NULL &&! "". Equals (selection)) {
  91. where = where + "and" + selection;
  92. }

  93. Return Db.query (tableName, projection, where, Selectionargs, Null,null, SortOrder);
  94. Default
  95. throw new IllegalArgumentException ("uri illegalargument:" + uri);
  96. }
  97. }

  98. @Override
  99. public int update (URI uri, contentvalues values, String selection,string[] selectionargs) {
  100. Sqlitedatabase db = This.openHelper.getWritableDatabase ();
  101. int result;
  102. Switch (Urimatcher.match (URI)) {
  103. Case Contact_list:
  104. result = Db.update (selection, values, selection, Selectionargs);
  105. Break
  106. Case CONTACT:
  107. Long id = Contenturis.parseid (URI);
  108. String where = "_id=" + ID;
  109. if (selection! = NULL &&! "". Equals (selection)) {
  110. where = where + "and" + selection;
  111. }

  112. result = Db.update (TableName, values, where, Selectionargs);
  113. Break
  114. Default
  115. throw new IllegalArgumentException ("uri illegalargument:" + uri);
  116. }
  117. return result;
  118. }

  119. }

Copy Code

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.