Content providers are not only static sources of data.
Create uri instance to locate default contacts along with value stored inside ContentValues.
Create uri instance to make new uri that points to this newly created contact.
Use ContentValues instance to insert further information for new contact using newly created uri.
Use insert() method of getContentResolver() to insert new contact.
Content providers are not only static sources of data.
They can also be used to add, update, and delete data, if the content provider application has implemented this functionality.
The return value is the Uri of our new contact, use it for adding phone numbers to our new contact.
Reuse the ContentValues instance by clearing it and adding a Contacts.Phones.NUMBER and the Contacts.Phones.TYPE for it.
Using the ContentResolver,we insert this data into the newly created Uri.
Create instance of ContentValues & use put() method for the columns where data needs to be updated.
Use update() method of getContentResolver() to update contact.
An instance of the ContentValues object to map the data field to update with the data value in this case, the note field.
Replaces any current note stored in the NOTES field currently stored with the contact.
Create the Uri for the specific contact we are updating, a simple
call to the update() method of the ContentResolver class completes our
change.
They can also be used to add, update, and delete data, if the content provider application has implemented this functionality.
Your application must have the appropriate permissions (that is, WRITE_CONTACTS as opposed to READ_CONTACTS) to perform some of these actions.
Insert new contact
Create instance of ContentValues. Use put method to put key/value pair for new contact.Create uri instance to locate default contacts along with value stored inside ContentValues.
Create uri instance to make new uri that points to this newly created contact.
Use ContentValues instance to insert further information for new contact using newly created uri.
Use insert() method of getContentResolver() to insert new contact.
Content providers are not only static sources of data.
They can also be used to add, update, and delete data, if the content provider application has implemented this functionality.
| Code: |
| ContentValues values = new ContentValues(); values.put(Contacts.People.NAME, “Sample User”); |
Insert the data in the database found at
the Contacts.People.CONTENT_URI – Use a call to getContentResolver() to
retrieve the ContentResolver associated with our Activity.
| Code: |
| Uri uri = getContentResolver().insert(Contacts.People.CONTENT_URI, values);
Uri phoneUri = Uri.withAppendedPath(uri,Contacts.People.Phones.CONTENT_DIRECTORY); |
Reuse the ContentValues instance by clearing it and adding a Contacts.Phones.NUMBER and the Contacts.Phones.TYPE for it.
Using the ContentResolver,we insert this data into the newly created Uri.
| Code: |
| values.clear();
values.put(Contacts.Phones.NUMBER, “2125551212”); values.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_WORK); getContentResolver().insert(phoneUri, values); |
Updating contact
Create new uri to point to contact that needs to be updated.Create instance of ContentValues & use put() method for the columns where data needs to be updated.
Use update() method of getContentResolver() to update contact.
An instance of the ContentValues object to map the data field to update with the data value in this case, the note field.
Replaces any current note stored in the NOTES field currently stored with the contact.
| Code: |
| ContentValues values = new ContentValues(); values.put(People.NOTES, “This is my boss”); |
| Code: |
| Uri updateUri = ContentUris.withAppendedId(People.CONTENT_URI, rowId);
int rows = getContentResolver().update(updateUri, values, null, null); Log.d(debugTag, “Rows updated: “ + rows); |
Deleting Records
- Deleting All Records
The delete() method deletes all rows at a
given URI filtered by the selection parameters, which, in this case,
includes all rows at the People.CONTENT_URI location; in other words,
all contact entries.
| Code: |
| int rows = getContentResolver().delete(People.CONTENT_URI, null, null); Log.d(debugTag, “Rows: “+ rows); |
- Deleting Specific Records
To select specific rows to delete by
adding the unique identifier index to the end of the URI or remove rows
matching a particular pattern.
For example, the following deletion matches all contact records with the name Sample User.| Code: |
| int rows = getContentResolver().delete(People.CONTENT_URI,
People.NAME + “=?”, new String*+ ,“Sample User”-); Log.d(debugTag, “Rows: “+ rows); |



Posted in: 
0 comments:
Post a Comment