Android Learning Notes (49): Accessing data via content provider

Source: Internet
Author: User
Tags wrapper

In our last note, we wrote our own provider, this note, we will access the data via the content provider URI interface and rewrite the example in the Android learning Note (42). Here we are not fully describing how the relevant UI is written and can be viewed in detail in notes (42), focusing on how to achieve data access.

Read Information

Read the information way, in the note (47) has been introduced, the code is as follows

private voidRead(){
/ * Read by managedquery, the 1th parameter represents the URI, the 2nd parameter represents the information you want to read, and the 3rd parameter is a constraint, similar to the Where in SQL, the 4th parameter and the 3rd parameter, which is supported in the third argument. "Exactly why; the 5th parameter is similar to an order by in SQL. If we are reading specific instance instead of collect, you can set the URI uri = Contenturis.withappendedid (gravityprovider.constants.content_uri,2); */
Cursor Cursor =Managedquery(Gravityprovider.constants.content_uri,projection,null,null,null);

if (adapter = = NULL) {
adapter = new Simplecursoradapter (This,r.layout.chapter_22_test1,
Cursor
New String[]{gravityprovider.constants.title,gravityprovider.constants.value},
New int[]{r.id.c22_name,r.id.c22_gravity});
Setlistadapter (adapter);
}else{
Adapter.changecursor (cursor);
Constantscursor.close ();
}
Constantscursor = cursor;
}

Synchronizing Information

Content provider can have a number of applications to access, the application can be used to delete data, other applications can also be deleted and additions to the data, provider provides a notification mechanism, when the URI points to the data changes, notify the client, allowing the client to synchronize in real time.

//Step 1 (A): Define the Contentobserver object that handles communications
PRIVATE Contentobserver observer = NULL;
//Step 3: Log off, make fun of a change in URI data notification
protected void OnPause () {
...... ......
Getcontentresolver ().Unregistercontentobserver(Observer);
Observer = NULL;
}
//Step 2: Register to require notification of data changes in a URI
protected void Onresume () {
...... ......
Observer = new Myobserver ();
Getcontentresolver ().Registercontentobserver(GravityProvider.Constants.CONTENT_URI, True,observer);
}
//Step 1 (B): Contentoberver is an abstract class, we need to define the processing of the notifications we receive
Private Class Myobserverextends Contentobserver{
Public Myobserver () {
Super (New Handler ());
}
//Specific description of received provider notification How to handle, in this case, just refresh the list can
public voidOnChange(Boolean Selfchange) {
Super.onchange (Selfchange);
Read ();
}
}

Add Data

The URI to increase the data must be Collectionuri, with insert () and Bulkinsert () two interfaces, the former being a instance, and the latter being through an array plus multiple.

private void AddData (Dialogwrapper wrapper) {
Contentvalues CV = new Contentvalues (2);
Cv.put (GravityProvider.Constants.TITLE, Wrapper.gettitle ());
Cv.put (GravityProvider.Constants.VALUE, wrapper.getgravity ());
getcontentresolver (). Insert (GravityProvider.Constants.CONTENT_URI, CV);
}

Delete Data

Delete Data takes a delete, can be a instance URI, or it can be a collection.

Uri uri = Contenturis.withappendedid (GravityProvider.Constants.CONTENT_URI, rowId);
Getcontentresolver (). Delete (URI, NULL, NULL);

Note BLOB

BLOBs (Binary Large Objects), such as some picture information, can be supported in many databases, including SQLite. Content provider cannot be accessed directly by cursor in these binary data, and getInputStream () and Getoutpurstream () can be used on ContentProvider to read and write binary information. For image,andriod do not need to copy the data out somewhere, and then display, you can directly implement the URI to display.

access from other applications

We can access data in a different activity in one application, and more often in other applications. After trial, data access was successful.


RELATED Links: My Android development related articles

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.