Selectors are intended for other applications to classify the URIs that are passed in to determine which table to manipulate,
The first thing to declare is Urimatcher
static Urimatcher um = new Urimatcher (urimatcher.no_match);
Then the description of the matching rule in the static block
Determine the matching rules in the block,
The first parameter is the host name, followed by the table name, followed by the result code, typically 1 start
Um.adduri ("Com.hello.demo", "people", 1);
Um.adduri ("Com.hello.demo", "Teacher", 2);
Path followed by a number match
Um.adduri ("Com.hello.demo", "people/#", 3);
Matches with characters after a path
Um.adduri ("Com.hello.demo", "people/*", 4);
And then you change Charlie. Start classifying URIs
@Override
Public URI insert (URI uri, contentvalues values) {
URI selector to select, return result code
Switch (Um.match (URI)) {
Case 1:
Db.insert ("People", null, values);
Break
Case 2:
Db.insert ("Teacher", null, values);
Break
Default
Break
}
return URI;
}
The other application must add a matching rule when it calls through the URI, otherwise the return value is 1.
Findviewbyid (R.ID.INSERT_BTN). Setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
Contentresolver CR = Getcontentresolver ();
Contentvalues values = new Contentvalues ();
Values.put ("name", "Tom");
Values.put ("Sex", "M");
Cr.insert (Uri.parse ("Content://com.hello.demo/people"), values);
}
});
Findviewbyid (R.ID.DELETE_BTN). Setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
Contentresolver CR = Getcontentresolver ();
Contentvalues values = new Contentvalues ();
Values.put ("Name", "Jim");
Cr.insert (Uri.parse ("Content://com.hello.demo/teacher"), values);
}
});
PackageCom.neusoft.demo;ImportAndroid.content.ContentProvider;Importandroid.content.ContentValues;ImportAndroid.content.UriMatcher;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.net.Uri; Public classMycontentproviderextendsContentProvider {Sqliteopenhelper oh; Sqlitedatabase DB; //URI Selector, if there are more than two tables in this database, we will use the URI selector to determine the other//which database the application is working on StaticUrimatcher um =NewUrimatcher (Urimatcher.no_match); Static { //determine the matching rules in the block,//The first parameter is the hostname, then the expression, followed by the result code, General 1 startUm.adduri ("Com.hello.demo", "people", 1); Um.adduri ("Com.hello.demo", "Teacher", 2);
Path followed by a number match
Um.adduri ("Com.hello.demo", "people/#", 3);
Matches with characters after a path
Um.adduri ("Com.hello.demo", "people/*", 4);
} @Override Public intDelete (Uri Uri, String selection, string[] selectionargs) {returnDb.delete ("People", Selection, Selectionargs); } @Override PublicString getType (Uri uri) {//Todo:implement this to handle requests for the MIME type of the data//At the given URI. Throw NewUnsupportedoperationexception ("not yet implemented"); } @Override Publicuri insert (URI uri, contentvalues values) {//URI selector to select, return result code Switch(Um.match (URI)) { Case1: Db.insert ("People",NULL, values); Break; Case2: Db.insert ("Teacher",NULL, values); Break; default: Break; } returnURI; } @Override Public BooleanOnCreate () {Oh=NewMyopenhelp (GetContext ()); DB=oh.getwritabledatabase (); return false; } @Override PublicCursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { Cursor cu= Db.query ("People", projection, selection, Selectionargs,NULL,NULL, SortOrder); returncu; } @Override Public intupdate (URI Uri, contentvalues values, String selection, string[] selectionargs) {returnDb.update ("People", values, selection, Selectionargs); }}
Other application calls
PackageCom.example.contentprovider2;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.content.ContentResolver;Importandroid.content.ContentValues;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Findviewbyid (R.ID.INSERT_BTN). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {contentresolver cr=Getcontentresolver (); Contentvalues Values=Newcontentvalues (); Values.put ("Name", "Tom"); Values.put ("Sex", "M"); Cr.insert (Uri.parse ("Content://com.hello.demo/people"), values); } }); Findviewbyid (R.ID.DELETE_BTN). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {contentresolver cr=Getcontentresolver (); Contentvalues Values=Newcontentvalues (); Values.put ("Name", "Jim"); Cr.insert (Uri.parse ("Content://com.hello.demo/teacher"), values); } }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}
Urimatcher URI Selector