Step by Step _android Development Course [6]_contentprovider Study

Source: Internet
Author: User

Focus on technology, enjoy life! --qq:804212028
Browse Links: http://blog.csdn.net/y18334702058/article/details/44624305

    • Topic: ContentProvider Learning

What is ContentProvider?

ContentProvider (content Provider) is one of the four components of Android. Mainly used to share data externally, that is, by contentprovider the data in the application to other applications access, other applications can be contentprovider to the data in the specified application. So if you want to implement data sharing between different applications, you have to use content provider.

Use of ContentProvider

1. The main methods are:

public boolean onCreate () is called when the ContentProvider is created
Public Cursor query (Uri, string[], String, string[], string) is used to query the ContentProvider of the specified Uri, returning a cursor
The public URI insert (URI, contentvalues) is used to add data to the ContentProvider of the specified URI
public int update (URI, Contentvalues, String, string[]) to update the data in the contentprovider of the specified Uri
public int Delete (Uri, String, string[]) to delete data from the ContentProvider of the specified Uri
Public String GetType (URI) used to return the MIME type of data in the specified Uri

* If the manipulated data belongs to the collection type, then the MIME type string should start with vnd.android.cursor.dir/.
For example: To get the URI for all person Records is Content://contacts/person, the MIME type string returned is "Vnd.android.cursor.dir/person".

* If the data to be manipulated belongs to non-collection type data, then the MIME type string should start with vnd.android.cursor.item/.
For example: To get the URI for the person record with ID 10 as CONTENT://CONTACTS/PERSON/10, the MIME type string returned should be "Vnd.android.cursor.item/person".

2. Use instance (get phone number in Address book):

 Public  class contentproviderdemo extends Activity {    @Override     Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);       Setcontentview (R.layout.main);    Displayrecords (); }Private void Displayrecords() {//The array contains all the fields to be returnedString columns[] =NewString[] {people.name, people.number};       Uri mcontacts = People.content_uri; Cursor cur = managedquery (mcontacts, columns,//data fields to return           NULL,//WHERE clause           NULL,//Parameters of the WHERE clause           NULL         //order-by clauses);if(Cur.movetofirst ()) {String name =NULL; String Phoneno =NULL; do {//Get the value of the fieldName = Cur.getstring (Cur.getcolumnindex (people.name));             Phoneno = cur.getstring (Cur.getcolumnindex (People.number)); Toast.maketext ( This, name + "" + Phoneno, Toast.length_long). Show (); } while(Cur.movetonext ()); }    }}

What do we do if we need to modify the information inside?
We can use the Contentresolver.update () method to modify the data, we first write a method to modify the data:

privatevoidupdateRecord(int recNo, String name) {    Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, recNo);    new ContentValues();    values.put(People.NAME, name);    nullnull);//利用ContentResolver.update()这个方法来修改数据}

Now we can call the above method to update the data:

updateRecord(10, “小嘟嘟”);   //更改第10条记录的name字段值为“小嘟嘟”

The implementation of other methods such as adding and deleting other data is similar.

Focus on technology, enjoy life! --qq:804212028
Browse Links: http://blog.csdn.net/y18334702058/article/details/44624305

Step by Step _android Development Course [6]_contentprovider Study

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.