I have a problem when try to update existing contact with field which is not exist.
Example:
I successfully create new contact which contain name and email address fields. During create action I use ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) only for fields which are not empty.
Code for inserting phone field during contact creation (works fine):
if (isNewContact){
if (!phone.equals("")){
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, id)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, type)
.build());
}
}
So, now I try to run update process and append to my contact the phone number:
if (isUpdateContact){
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND "
+ ContactsContract.CommonDataKinds.Phone.TYPE
+ "=? AND " + ContactsContract.Data.MIMETYPE
+ "=?",
new String[] {"" + id, "" + type,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE })
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, type)
.build());
}
In case if during contact creation the phone field was inserted, then update works fine. In case if phone was not inserted, then update does nothing - the field will not be updated. No warnings neither errors in LogCat.
The reason why I do not want to create empty fields on contact creation is because of empty fields crashes Phone application in Samsung Nexus device when I switch to contacts tab with null pointer exception in com.android.contacts.list.ContactListItemView.onMeasure(ContactListItemView.java:350).
So, my question is:
How during update process to insert a new field if not exist (and how to detect it) OR delete the field if exist but new update value is empty? Is it possible at all or there is another solution to avoid crash of Nexus phone app if fields are empty?
P.S. tried to run newInsert during for already created contact, getting:
mType: 1, mUri: content://com.android.contacts/data, mSelection: null, mExpectedCount: null, mYieldAllowed: false, mValues: data1= mimetype=vnd.android.cursor.item/phone_v2 data2=1, mValuesBackReferences: raw_contact_id=2962, mSelectionArgsBackReferences: null
java.lang.ArrayIndexOutOfBoundsException: asked for back ref 2962 but there are only 1 back refs
at android.content.ContentProviderOperation.backRefToValue(ContentProviderOperation.java:362)
Ok, I found the fix:
(this will do right update operation - in case if not found, create; in case if new value empty, just remove the field; in case if exists, do update.)
if (isUpdateContact){
ops.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND "
+ ContactsContract.CommonDataKinds.Phone.TYPE
+ "=? AND " + ContactsContract.Data.MIMETYPE
+ "=?",
new String[] {"" + id, "" + type,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE })
.build());
if (!phone.equals("")){
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, id)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, type)
.build());
}
}
Related
I am having issue where i need to have update Multiple Mobile no's , Landline no's and Emails , websites , address in local phonebook contact.
If contact is already exist in phone book then i am trying to update it's details.
Below is the code i am trying. It is getting executed without error but no multiple mobile no , landline , email etc not reflecting in phone-book on that contact as the multiple data's are their.
I also referred few of the links but that didn't helped as well.
String whereMobile = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = ? ";
String[] paramsMobile = new String[]{String.valueOf(contactID),
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};
if (userInfoMobileNos != null && !userInfoMobileNos.isEmpty()) {
for (int iUserMobile = 0; iUserMobile < userInfoMobileNos.size(); iUserMobile++) {
operationList.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(whereMobile, paramsMobile)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, userInfoMobileNos.get(iUserMobile).getPhoneNumber())
.build());
}
}
String whereGmail = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Email.TYPE + " = ? ";
String[] paramsGmail = new String[]{String.valueOf(contactID),
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_WORK)};
if (userInfoGmails != null && !userInfoGmails.isEmpty()) {
for (int iUserGmail = 0; iUserGmail < userInfoGmails.size(); iUserGmail++) {
operationList.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(whereGmail, paramsGmail)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, userInfoGmails.get(iUserGmail).getEmail())
.build());
}
}
Updated code
private void updateNew(Context context, int rawContactID) {
ArrayList<ContentProviderOperation> operationList = new ArrayList<>();
String whereMobile = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = ? ";
String[] paramsMobile = new String[]{String.valueOf(rawContactID),
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};
// First delete all the existing phones with type mobile, if any exist
operationList.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(whereMobile, paramsMobile)
.build());
// Next, insert all the updated phones:
for (MobileNumbersItem infoMobileNo : userInfoMobileNos) { // <== change the class to the one you're using in userInfoMobileNos
operationList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) // <== insert not update!
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, infoMobileNo.getPhoneNumber())
.build());
}
String whereGmail = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Email.TYPE + " = ? ";
String[] paramsGmail = new String[]{String.valueOf(rawContactID),
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_WORK)};
// First delete all the existing emails with type work, if any exist
operationList.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(whereGmail, paramsGmail)
.build());
// Next, insert all the updated emails:
for (GmailsItem userInfoGmail : userInfoGmails) { // <== change the class here too
operationList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, userInfoGmail.getEmail())
.build());
}
try {
// don't forget to apply the operations now:
ContentProviderResult[] results = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);
Log.d("UPDATE CONTACRT", "results=" + Arrays.toString(results));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("updateContactError", Objects.requireNonNull(e.getMessage()));
}
}
Any help will be appreciated here. I have tried to debug but can't able to find the issue.
one issue that can be either a bad parameter name, or an actual bug is that your selection is requesting a RAW_CONTACT_ID and supplying a parameter called contactId, I'm not sure what is stored in contactId but there's a big difference between a rawContactId and a contactId, so if that's indeed a contactId your operations will not find anything to update (or even worse, update the wrong contact).
The second issue is that your two loops (over userInfoMobileNos and userInfoGmails) keep updating (overriding) the same Data row, so eventually you should end up with just the last value in the contact details.
You're also not checking if there's a Data row in the contact details that fit your selection, if there's not, there will be no row to update, and therefore the code will simply do nothing.
Third issue, but it may be intentional, not sure how you expect your app to work, is that you assume the info item type, i.e. you only update a phone if it's TYPE_MOBILE, and you only update the email if it's TYPE_WORK, what if there are other phones and emails stored for that contact? your code will simply keep those untouched, not sure if that intentional or not.
To fix issue one, just make sure the value in contactId is a RawContactId and change the name of the param.
For the second issue, you should instead do a delete-and-insert, see code below.
For the third issue, you'll need to change the selection code (paramsMobile) to remove it's assumption of label.
Here's suggested code:
String whereMobile = Data.RAW_CONTACT_ID + " = ? AND " +
Data.MIMETYPE + " = ? AND " +
Phone.TYPE + " = ? ";
String[] paramsMobile = new String[]{String.valueOf(rawContactID),
Phone.CONTENT_ITEM_TYPE,
String.valueOf(Phone.TYPE_MOBILE)};
// First delete all the existing phones with type mobile, if any exist
operationList.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(whereMobile, paramsMobile)
.build());
// Next, insert all the updated phones:
for (UserInfo infoMobileNo : userInfoMobileNos) { // <== change the class to the one you're using in userInfoMobileNos
operationList.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) // <== insert not update!
operationList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) // <== insert not update!
.withValue(Data.RAW_CONTACT_ID, rawContactID)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.TYPE, Phone.TYPE_MOBILE)
.withValue(Phone.NUMBER, infoMobileNo.getPhoneNumber())
.build());
}
String whereGmail = Data.RAW_CONTACT_ID + " = ? AND " +
Data.MIMETYPE + " = ? AND " +
Email.TYPE + " = ? ";
String[] paramsGmail = new String[]{String.valueOf(rawContactID),
Email.CONTENT_ITEM_TYPE,
String.valueOf(Email.TYPE_WORK)};
// First delete all the existing emails with type work, if any exist
operationList.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(whereGmail, paramsGmail)
.build());
// Next, insert all the updated emails:
for (UserInfo userInfoGmail : userInfoGmails) { // <== change the class here too
operationList.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, rawContactID)
.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(Email.TYPE, Email.TYPE_WORK)
.withValue(Email.DATA, userInfoGmail.getEmail())
.build());
}
// don't forget to apply the operations now:
ContentProviderResult[] results = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);
Log.d("UPDATE CONTACRT", "results=" + Arrays.toString(results));
I am tried to update multiple phone numbers of specific contact using following
code:
for(int j=0;j<allPhoneNumbersLength;j++)
{
PhoneInfo phoneInfo = (PhoneInfo) allPhoneNumbers.elementAt(j);
String phoneValue = phoneInfo.getValue();
int phoneType = phoneInfo.getIndex(); // phoneType = Phone.TYPE_HOME, Phone.TYPE_WORK, etc
ContentProviderOperation.Builder builderPhone = ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=?"+" AND "+ContactsContract.Data.MIMETYPE + "=?" + " AND "+Phone.TYPE+"=?", new String[]{String.valueOf(contactID), Phone.CONTENT_ITEM_TYPE, String.valueOf(phoneType)});
if(phoneType == Phone.TYPE_HOME)
{
builderPhone.withValue(Phone.NUMBER, phoneValue)
.withValue(Phone.TYPE, Phone.TYPE_HOME);
}
else if(phoneType == Phone.TYPE_WORK)
{
builderPhone.withValue(Phone.NUMBER, phoneValue)
.withValue(Phone.TYPE, Phone.TYPE_WORK);
}
else if(phoneType == Phone.TYPE_FAX_HOME)
{
builderPhone.withValue(Phone.NUMBER, phoneValue)
.withValue(Phone.TYPE, Phone.TYPE_FAX_HOME);
}
op_list.add(builderPhone.build());
}
getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
Using this code I am trying to update three numbers, but only "TYPE_FAX_HOME"
number is updated and other two numbers are removed from contact.
Please help me.
I've learnt from your code that, you're using the same phone number value for all the 3 types. Hence, while displaying, android will display only 1 of them for the contact. But if you actually edit the contact, there you can see that, all the 3 types have been populated with the same number.
P.S: I'm assuming that the contact for which you're trying to do the edit, already has some number populated for all the 3 types. If not, please create them and then try running your code.
Wow ... ! why you use the "if" statement ?!
your code could be like this :
for(int j=0;j<allPhoneNumbersLength;j++)
{
PhoneInfo phoneInfo = (PhoneInfo) allPhoneNumbers.elementAt(j);
int phoneType = phoneInfo.getIndex(); // phoneType = Phone.TYPE_HOME, Phone.TYPE_WORK, etc
ContentProviderOperation.Builder builderPhone = ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=?"+" AND "+ContactsContract.Data.MIMETYPE + "=?" + " AND "+Phone.TYPE+"=?", new String[]{String.valueOf(contactID), Phone.CONTENT_ITEM_TYPE, String.valueOf(phoneType)});
builderPhone.withValue(Phone.NUMBER, phoneValue)
.withValue(Phone.TYPE, phoneType);
op_list.add(builderPhone.build());
}
getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
I'm not Android programmer but i think the problem is in last line of code , the loop update the contact info in each step but not apply it , at the end step of the loop apply update and the last changes should be apply , if you change your code like this i think it should work , it means for each loop's step the contact info will update :
for(int j=0;j<allPhoneNumbersLength;j++)
{
PhoneInfo phoneInfo = (PhoneInfo) allPhoneNumbers.elementAt(j);
int phoneType = phoneInfo.getIndex(); // phoneType = Phone.TYPE_HOME, Phone.TYPE_WORK, etc
ContentProviderOperation.Builder builderPhone = ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=?"+" AND "+ContactsContract.Data.MIMETYPE + "=?" + " AND "+Phone.TYPE+"=?", new String[]{String.valueOf(contactID), Phone.CONTENT_ITEM_TYPE, String.valueOf(phoneType)});
builderPhone.withValue(Phone.NUMBER, phoneValue)
.withValue(Phone.TYPE, phoneType);
op_list.add(builderPhone.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
}
Problem with Android Emulator. Code works fine in real device.
Thanks for all your help.
I'm trying to modify contact first name and last name programmatically.
The code snippet that I've used in order to do the job is the following one:
operations.add( ContentProviderOperation.newUpdate( Data.CONTENT_URI )
.withSelection( RawContacts._ID + "=?",
new String[] { String.valueOf( mSmartphoneContactKey) } )
.withValue( ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
mContactName.getEditableText().toString() )
.withValue( ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
mContactLastName.getEditableText().toString() )
.build() );
The mSmartphoneContactKey is filled in with the data contained in the column
ContactsContract.Contacts._ID
which is sitting in my projection array when I read contacts using content provider.
The problem is that for some contacts the name and last name are not modified and the phone type is modified instead.
Actually I don't have any clue about the cause.
Any advice is appreciated.
I've read further the documentation the Data table is the one I have to use.
I've modified the code as below...still not working
operations.add( ContentProviderOperation.newUpdate( Data.CONTENT_URI )
.withSelection( Data._ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'",
new String[] { String.valueOf( mSmartphoneContactId ) } )
.withValue( ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, mContactName.getEditableText().toString() )
.withValue( ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, mContactLastName.getEditableText().toString() )
.build() );
Please help me!
Ok Solved!
Wrong ID passed.
Need to retrieve the ID along the data from the DATA table.
I have an app that can add and delete contacts just fine. And if there is an existing value in an existing contact, it can be modified just fine. But I can't seem to be able to insert new values into existing contacts. For example, if there is an existing value for a home phone number, but not for the work phone number, I tried using the fillowing to add the value (cintact idValue and workNumber are passed in):
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.CONTACT_ID, idValue)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
But I get a NullPointerException:
java.lang.NullPointerException
at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:2604)
at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2452)
at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:106)
at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2256)
at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
at com.android.providers.contacts.SQLiteContentProvider.applyBatch(SQLiteContentProvider.java:216)
at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2290)
at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:217)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:194)
at android.os.Binder.execTransact(Binder.java:336)
at dalvik.system.NativeStart.run(Native Method)
Can someone please tell me what I'm doing wrong?
You are missing withValue(Phone.TYPE, Phone.TYPE_WORK)
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, idValue)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber).
withValue(Phone.TYPE, Phone.TYPE_WORK)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Edits
You are correct I checked the schema for the data table and it is as follow it is only using raw contacts id and not contact id
CREATE TABLE data (_id INTEGER PRIMARY KEY AUTOINCREMENT,
package_id INTEGER REFERENCES package(_id),
mimetype_id INTEGER REFERENCES mimetype(_id) NOT NULL,
raw_contact_id INTEGER REFERENCES raw_contacts(_id) NOT NULL,
is_primary INTEGER NOT NULL DEFAULT 0,
is_super_primary INTEGER NOT NULL DEFAULT 0,
data_version INTEGER NOT NULL DEFAULT 0,
data1 TEXT,
data2 TEXT,
data3 TEXT,
data4 TEXT,
data5 TEXT,
data6 TEXT,
data7 TEXT,
data8 TEXT,
data9 TEXT,
data10 TEXT,
data11 TEXT,
data12 TEXT,
data13 TEXT,
data14 TEXT,
data15 TEXT,
data_sync1 TEXT,
data_sync2 TEXT,
data_sync3 TEXT,
data_sync4 TEXT );
You were getting error because Android doesn't give you access to add a contact directly to you Contact table. Rather you need to modify or add a raw Contact and Android will automatically create a Contact for you.
I have a proplem when I make a app "contacts manager" for android.
I have processed creating and deleting a contact with name, and phone
but when I want to add more a home email info to an existing contacts but can't do that.
here my code:
ArrayList ops = new ArrayList();
String selectName = ContactsContract.Data.CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "= ? ";
String[] NameArgs = new String[]{strId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
String selectPhone = ContactsContract.Data.CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "= ? ";
String[] phoneArgs = new String[]{strId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
String selectEmail = ContactsContract.Data.CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "= ? ";
String[] emailArgs = new String[]{strId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
String selectPostAdd = ContactsContract.Data.CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "= ? AND " + ContactsContract.CommonDataKinds.StructuredPostal.TYPE + "= ? ";
String[] postAddArgs = new String[]{strId, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, String.valueOf(ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)};
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectName, NameArgs)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, updContact_name.getText().toString())
.build());
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, updContact_phone.getText().toString())
.build());
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectEmail, emailArgs)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, updContact_textMail.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPostAdd, postAddArgs)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.DATA, updContact_textPostAdd.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
this code is available for update a contacts but an Item Info(here is email, StructruedPostal) has been created once. but when I create it with contacts app of android with only name, number (don't touch email, structuedPostal) and save, I don't update it in my android app.
somebody have experience about this pls help me! tks
sorry about my grammar english! It have more mistake and wrong grammar. :(. Tks all
Updating will not insert field which doesn't exist, so follow these steps.
First query for the raw Contact Id of your Contact.
use new Insert method to insert data that has not been inserted to the contact previously when created (in the oppt case newUpdate() will work fine).
A piece of code goes like this
String[] params = new String[] {"Give your Contact"};// you need query and get this too.
String[] proj = {ContactsContract.RawContacts._ID};
Cursor c = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,proj,
ContactsContract.Data._ID +"= ?", params, null);
long rawContactId = -1;
while(c.moveToNext()){
rawContactId = c.getLong(0);
}
c.close();
After getting those values,
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, data.getEmail2Address())
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME)
.build());