Android Content Providers (2)

Source: Internet
Author: User

Read the data read from the query. Reading retrieved data

The cursor object returned by the query can be used to access the result record set. If you query by a specified ID, this set has only one value. Otherwise, it can contain multiple values. (If no matching result exists, it may be null .) You can read data from a specific field in the table, but you must know the Data Type of this field, this cursor object has a separate reading method for each data type, such as getString (), getInt (), and getFloat (). (However, for most types, if you call the string reading method, the cursor object will return a string representation of this data .) A cursor allows you to request a column name by column index or by column name.

The following code snippet demonstrates how to read the name and phone number from the preceding query results:

Java code:

  1. Import android. provider. Contacts. People;
  2. Private void getColumnData (Cursor cur ){
  3. If (cur. moveToFirst ()){
  4. String name;
  5. String phoneNumber;
  6. Int nameColumn = cur. getColumnIndex (People. NAME );
  7. Int phoneColumn = cur. getColumnIndex (People. NUMBER );
  8. String imagePath;
  9. Do {
  10. // Get the field values
  11. Name = cur. getString (nameColumn );
  12. PhoneNumber = cur. getString (phoneColumn );
  13. // Do something with the values.
  14. ...
  15. } While (cur. movetonext ());
  16. }
  17. }

Copy code

If a query may return binary data, such as an image or sound, the data may be directly input into a table or table entry, or a string of content: URI can be used to obtain the data, generally, small data (for example, 20 to 50 K or smaller) is most likely to be directly stored in a table. You can call Cursor. getBlob. It returns a byte array.

If the table entry is a content: URI, you shouldn't try to open and read the file directly (it will fail due to permission issues ). Instead, you should call ContentResolver. openInputStream () to obtain an InputStream object. You can use it to read data.

Modify data modifying data
You can modify the data stored in the content provider using the following methods:
Add new records
Add new data for existing records
Batch update existing records
Delete record
All data modification operations are completed by using the ContentResolver method. Some content providers require a stronger permission constraint on writing data than reading data. If you do not have the write permission for the content provider, the ContentResolver method will fail.

Add record adding records
To add a new record to a content provider, the first step is to build a key-Value Pair ing in the ContentValues object, here, a column name for each key and content provider matches and the value is the expected value of the column in the new record. Then call ContentResolver. insert () and pass the URI of the provided tool to the ContentValues ing graph. This method returns the full name of the URI of the new record-that is, the URI of the content provider plus the extension ID of the new record. You can use this URI to query and obtain a cursor on the new record, and then modify the record. The following is an example:

Java code:

  1. Import Android. provider. Contacts. People;
  2. Import Android. content. contentresolver;
  3. Import Android. content. contentvalues;
  4. Contentvalues values = new contentvalues ();
  5. // Add Abraham Lincoln to contacts and make him a favorite.
  6. Values. Put (people. Name, "Abraham Lincoln ");
  7. // 1 = the new contact is added to favorites
  8. // 0 = the new contact is not added to favorites
  9. Values. Put (people. starred, 1 );
  10. Uri uri = getcontentresolver (). insert (people. content_uri, values );

Copy code

Add new value adding new values
Once a record already exists, you can add new information or modify existing information. For example, the next step in the preceding example is to add contact information, such as a phone number or an IM or email address, to a new entry.

The best way to add a record to the contact database is to extend the table name after the record URI, and then use the modified URI to add new data values. Therefore, each contact table exposes a table name of the CONTENT_DIRECTORY constant. The following code continues the previous example by adding a phone number and email address for the record just created above:

Java code:

  1. Uri phoneuri = NULL;
  2. Uri emailuri = NULL;
  3. // Add a phone number for Abraham Lincoln. Begin with the URI
  4. // The new record just returned by insert (); it ends with the _ ID
  5. // Of the new record, so we don't have to add the ID ourselves.
  6. // Then append the designation for the phone table to this URI,
  7. // And use the resulting URI to insert the phone number.
  8. PhoneUri = Uri. withAppendedPath (uri, People. Phones. CONTENT_DIRECTORY );
  9. Values. clear ();
  10. Values. put (People. Phones. TYPE, People. Phones. TYPE_MOBILE );
  11. Values. put (People. Phones. NUMBER, "1233214567 ");
  12. GetContentResolver (). insert (phoneUri, values );
  13. // Now add an email address in the same way.
  14. EmailUri = Uri. withAppendedPath (uri, People. ContactMethods. CONTENT_DIRECTORY );
  15. Values. clear ();
  16. // ContactMethods. KIND is used to distinguish different kinds
  17. // Contact methods, such as email, IM, etc.
  18. Values. put (People. ContactMethods. KIND, Contacts. KIND_EMAIL );
  19. Values. put (People. ContactMethods. DATA, "test@example.com ");
  20. Values. put (People. ContactMethods. TYPE, People. ContactMethods. TYPE_HOME );
  21. GetContentResolver (). insert (emailUri, values );

Copy code

You can put a small amount of binary data into a table by calling the ContentValues. put () version of the received byte stream. This is feasible for data such as small icons or short audio clips. However, if you need to add a large amount of binary data, such as a photo or a complete song, you need to put the content: URI of the data in the table and call ContentResolver with the URI of the file. openOutputStream () method. (This causes the content provider to save the data in a file and record the file path in a hidden field of the record .)

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.