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:
- The constant Urimatcher.no_match represents a return code (-1) that does not match any path.
- Urimatcher urimatcher = new Urimatcher (urimatcher.no_match);
- If the match () method matches the Content:.sqlite.provider.contactprovider/contact path, a match code of 1 is returned
- Urimatcher.adduri ("Com.changcheng.sqlite.provider.contactprovider", "Contact", 1);
- Add need to match URI, if match will return match code
- If the match () method matches the content.sqlite.provider.contactprovider/contact/230 path, a match code of 2 is returned
- Urimatcher.adduri ("Com.changcheng.sqlite.provider.contactprovider", "contact/#", 2);
- #号为通配符
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:
- Package eoe.sqlite.provider;
- Import Com.changcheng.sqlite.MyOpenHelper;
- Import Android.content.ContentProvider;
- Import Android.content.ContentUris;
- Import android.content.ContentValues;
- Import Android.content.UriMatcher;
- Import Android.database.Cursor;
- Import Android.database.sqlite.SQLiteDatabase;
- Import Android.net.Uri;
- public class Contactcontentprovider extends ContentProvider {
- Matching external requests via Urimatcher
- private static Urimatcher Urimatcher = new Urimatcher (urimatcher.no_match);
- Database read and write via Openhelper
- Private Myopenhelper Openhelper;
- Match State constants
- private static final int contact_list = 1;
- private static final int contact = 2;
- Table name
- private static final String TableName = "Contacts";
- Add URI
- static {
- Urimatcher.adduri ("Com.changcheng.sqlite.provider", "Contact", contact_list);
- Urimatcher.adduri ("Com.changcheng.sqlite.provider", "contact/#", contact);
- }
- @Override
- public int Delete (URI Uri, String selection, string[] Selectionargs) {
- Sqlitedatabase db = This.openHelper.getWritableDatabase ();
- int result;
- Switch (Urimatcher.match (URI)) {
- Case Contact_list:
- result = Db.delete (TableName, Selection, Selectionargs);
- Break
- Case CONTACT:
- Long id = Contenturis.parseid (URI);
- String where = "_id=" + ID;
- if (selection! = NULL &&! "". Equals (selection)) {
- where = where + "and" + selection;
- }
- result = Db.delete (TableName, where, Selectionargs);
- Break
- Default
- throw new IllegalArgumentException ("uri illegalargument:" + uri);
- }
- return result;
- }
- @Override
- Public String GetType (Uri uri) {
- Switch (Urimatcher.match (URI)) {
- Case contact_list://The collection type must precede the vnd.android.cursor.dir/
- return "Vnd.android.cursor.dir/contactlist";
- Case contact://The non-collection type must precede the vnd.android.cursor.item/
- return "Vnd.android.cursor.item/contact";
- Default
- throw new IllegalArgumentException ("uri illegalargument:" + uri);
- }
- }
- @Override
- Public URI insert (URI uri, contentvalues values) {
- Sqlitedatabase db = This.openHelper.getWritableDatabase ();
- Long ID;
- Switch (Urimatcher.match (URI)) {
- Case Contact_list:
- 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!
- id = Db.insert (tableName, "_id", values);
- Return Contenturis.withappendedid (URI, id);
- Case CONTACT:
- id = Db.insert (tableName, "_id", values);
- String Uripath = uri.tostring ();
- String path = uripath.substring (0, Uripath.lastindexof ("/")) + ID;
- return Uri.parse (path);
- Default
- throw new IllegalArgumentException ("uri illegalargument:" + uri);
- }
- }
- @Override
- public Boolean onCreate () {
- This.openhelper = new Myopenhelper (This.getcontext ());
- return true;
- }
- @Override
- Public Cursor query (URI uri, string[] projection, String selection,
- String[] Selectionargs, String sortOrder) {
- Sqlitedatabase db = This.openHelper.getWritableDatabase ();
- Switch (Urimatcher.match (URI)) {
- Case Contact_list:
- Return Db.query (TableName, projection, selection, selectionargs,null, NULL, sortOrder);
- Case CONTACT:
- Long id = Contenturis.parseid (URI);
- String where = "_id=" + ID;
- if (selection! = NULL &&! "". Equals (selection)) {
- where = where + "and" + selection;
- }
- Return Db.query (tableName, projection, where, Selectionargs, Null,null, SortOrder);
- Default
- throw new IllegalArgumentException ("uri illegalargument:" + uri);
- }
- }
- @Override
- public int update (URI uri, contentvalues values, String selection,string[] selectionargs) {
- Sqlitedatabase db = This.openHelper.getWritableDatabase ();
- int result;
- Switch (Urimatcher.match (URI)) {
- Case Contact_list:
- result = Db.update (selection, values, selection, Selectionargs);
- Break
- Case CONTACT:
- Long id = Contenturis.parseid (URI);
- String where = "_id=" + ID;
- if (selection! = NULL &&! "". Equals (selection)) {
- where = where + "and" + selection;
- }
- result = Db.update (TableName, values, where, Selectionargs);
- Break
- Default
- throw new IllegalArgumentException ("uri illegalargument:" + uri);
- }
- return result;
- }
- }
Copy Code