Reputation: 1423
How can I add a phone number into existing contact in my contactlist programmatically? I know how to add or delete contact, but i cannt add phone number into one of the contacts... So, help me please.
Upvotes: 2
Views: 540
Reputation: 31
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
//...
//add Phone to existiong Contact
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(Phone.NUMBER, delta.getAsString(Phone.NUMBER))
.withValue(Phone.TYPE, delta.getAsString(Phone.TYPE)).build());
//...
//add Email to existiong Contact
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(Email.ADDRESS, delta.getAsString(Email.ADDRESS))
.withValue(Email.TYPE, delta.getAsString(Email.TYPE)).build());
//...
try {
mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Log.d(TAG, "update success");
} catch (Exception e) {
Log.d(TAG, "update failed");
e.printStackTrace();
}
rawContactId
is the android.provider.ContactsContract.RawContacts._ID
you can query with Contacts.CONTENT_URI
to get this
delta
is RawContactDelta
, you can replace delta.getAsString(Phone.NUMBER)
, delta.getAsString(Email.ADDRESS)
with any String value
Upvotes: 3
Reputation: 330
Here is the Example.
try {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(ContactsContract.CommonDataKinds.Phone._ID + "=? AND " +
Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'",
new String[]{contact_id})
.withValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, "anything")
.build());
ContentProviderResult[] result = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
}
Upvotes: 0