Android development path 13 ---- ContentProvider connection

Source: Internet
Author: User
ContentProvider

1. PersonProvider

Package cn. class3g. db;

 

Import cn. class3g. service. DatabaseHelper;

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 PersonProvider extends ContentProvider {

Private static UriMatcher matcher = new UriMatcher (UriMatcher. NO_MATCH );

 

Private static final int PERSONS = 1;

Private static final int PERSON = 2;

 

Private DatabaseHelper dbHelper;

 

Static {

Matcher. addURI ("cn. class3g. providers. personprovider", "person", PERSONS );

Matcher. addURI ("cn. class3g. providers. personprovider", "person /#",

PERSON );

}

 

Public boolean onCreate (){

DbHelper = new DatabaseHelper (this. getContext ());

Return true;

}

 

// Content: // cn. itcast. provides. personprovider/person

Public Uri insert (Uri uri, ContentValues values ){

SQLiteDatabase db = dbHelper. getWritableDatabase ();

Long rowId;

 

Switch (matcher. match (uri )){

Case PERSONS: // Add a new record to the table and return its row number

RowId = db. insert ("person", "personid", values );

Return ContentUris. withAppendedId (uri, rowId );

Default:

Throw new IllegalArgumentException ("Unknow Uri:" + uri );

}

}

 

Public Cursor query (Uri uri, String [] projection, String selection,

String [] selectionArgs, String sortOrder ){

SQLiteDatabase db = dbHelper. getReadableDatabase ();

Switch (matcher. match (uri )){

Case PERSONS:

Return db. query ("person", projection, selection, selectionArgs, null, null, sortOrder );

Case PERSON:

Long personid = ContentUris. parseId (uri );

String where = "personid =" + personid;

If (selection! = Null &&! "". Equals (selection )){

Where = where + "and" + selection;

}

Return db. query ("person", projection, where, selectionArgs, null, null, sortOrder );

Default:

Throw new IllegalArgumentException ("Unknown Uri:" + uri );

}

}

 

// Content: // CN. itcast. Provides. personprovider/person update all records in the table

// Content: // CN. itcast. Provides. personprovider/person/10 update the record of the specified ID in the table

Public int Update (URI Uri, contentvalues values, string selection,

String [] selectionargs ){

Sqlitedatabase DB = dbhelper. getwritabledatabase ();

Int num;

Switch (matcher. match (uri )){

Case PERSONS: // update a specified record

Num = db. update ("person", values, selection, selectionArgs );

Break;

Case PERSON:

Long personid = ContentUris. parseId (uri );

String where = "personid =" + personid;

If (selection! = Null ){

Where + = "and" + selection;

}

Num = db. update ("person", values, where, selectionArgs );

Break;

Default:

Throw new IllegalArgumentException ("Unknow Uri" + uri );

}

Return num;

}

 

Public int delete (Uri uri, String selection, String [] selectionArgs ){

SQLiteDatabase db = dbHelper. getWritableDatabase ();

Int num = 0;

Switch (matcher. match (uri )){

Case PERSONS:

Num = db. delete ("person", selection, selectionArgs );

Break;

Case PERSON:

Long personid = ContentUris. parseId (uri );

String where = "personid =" + personid;

If (selection! = Null &&! "". Equals (selection )){

Where = where + "and" + selection;

}

Num = db. delete ("person", where, selectionArgs );

Break;

Default:

Throw new IllegalArgumentException ("Unknown Uri:" + uri );

}

Return num;

}

 

Public String getType (Uri uri ){

Switch (matcher. match (uri )){

Case PERSONS:

Return "vnd. android. cursor. dir/person ";

Case PERSON:

Return "vnd. android. cursor. item/person ";

Default:

Throw new IllegalArgumentException ("Unknown Uri:" + uri );

}

}

}

2. Configuration

<Provider

Android: Authorities ="Cn. class3g. providers. personprovider"

Android: Name ="Personprovider">

</Provider>

3. Create a test project and compile the test code.

PackageCN. class3g. Visitor;

 

ImportAndroid. content. contentresolver;

ImportAndroid. content. contentvalues;

ImportAndroid. database. cursor;

ImportAndroid.net. Uri;

ImportAndroid. Test. androidtestcase;

ImportAndroid. util. log;

 

Public classAccesscontentprovidertestExtendsAndroidtestcase {

Public voidTestsave ()ThrowsThrowable {

ContentResolver resolver =This. GetContext (). getContentResolver ();

Uri insertUri = Uri.Parse("Content: // cn. class3g. providers. personprovider/person ");

ContentValues values =NewContentValues ();

Values. put ("name", "laozhang ");

Values. put ("age", "50 ");

Uri uri = resolver. insert (insertUri, values );

Log.I("TAG", uri. toString ());

}

Public voidTestQuery ()ThrowsThrowable {

ContentResolver resolver =This. GetContext (). getContentResolver ();

Uri uri = Uri.Parse("Content: // cn. class3g. providers. personprovider/person ");

Cursor cursor = resolver. query (uri,Null,
Null,Null, "Personid asc ");

While(Cursor. moveToNext ()){

IntPersonid = cursor. getInt (cursor. getColumnIndex ("personid "));

String name = cursor. getString (cursor. getColumnIndex ("name "));

Log.I("TAG", "personid =" + personid + ", name =" + name );

}

Cursor. close ();

}

Public voidTestUpdate ()ThrowsThrowable {

ContentResolver contentResolver =This. GetContext (). getContentResolver ();

Uri updateUri = Uri.Parse("Content: // cn. class3g. providers. personprovider/person/5 ");

ContentValues values =NewContentValues ();

Values. Put ("name", "Chiang Kai-shek ");

Contentresolver. Update (updateuri, values,Null,
Null);

}

Public voidTestdelete ()ThrowsThrowable {

Contentresolver =This. Getcontext (). getcontentresolver ();

Uri uri = Uri.Parse("Content: // CN. class3g. providers. personprovider/person/5 ");

Contentresolver. Delete (Uri,Null,
Null
);

}

}

 

 

4. Test (deploy the provider owner project to the device first)

5. ContentProvider listener

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.