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

Source: Internet
Author: User

In the last note we wrote our own provider, this note, we will access the data via the content provider URI interface, rewriting 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 implement data access.

Reading information

Read the information 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 required read information, the 3rd parameter is a constraint, similar to where in SQL, the 4th parameter and the 3rd parameter are used together, specifically support the "?" in the third parameter. The 5th parameter resembles 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 be accessed by multiple applications, applications can delete data, other applications can also delete data, provider provides a notification mechanism, when the URI point to the data changes, notify the client, allowing the client to synchronize in real time.

//Step 1 (A): Define the Contentobserver object that handles communication
PRIVATE Contentobserver observer = NULL;
//Step 3: Log out and make fun of notification that a URI data has changed
protected void OnPause () {
...... ......
Getcontentresolver ().Unregistercontentobserver(Observer);
Observer = NULL;
}
//Step 2: Register to notify when data for a URI is changed
protected void Onresume () {
...... ......
Observer = new Myobserver ();
Getcontentresolver ().Registercontentobserver(GravityProvider.Constants.CONTENT_URI, True,observer);
}
//Step 1 (B): Contentoberver is an abstract class and we need to define the handling of the notifications we receive
Private Class Myobserverextends Contentobserver{
Public Myobserver () {
Super (New Handler ());
}
//Describe how the notification received by provider is handled, in this case simply refresh the list
public voidOnChange(Boolean Selfchange) {
Super.onchange (Selfchange);
Read ();
}
}

Add data

The URI for increasing the data must be Collectionuri, with insert () and Bulkinsert () two interfaces, the former being plus a instance, and the latter by adding more than one array.

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

Deleting data takes delete, which can be either a instance URI or 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 through the cursor in these binary data, and can be used to read and write binary information on ContentProvider using getInputStream () and Getoutpurstream (). For Image,andriod, you do not need to copy the data out somewhere, and then display it, you can directly implement the URI to display.

Access from other apps

We can make data access to different activity in one app, and when it's more common, it's accessed in other apps. After testing, data access was successful.

RELATED Links: My Android development related articles

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

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.