Android Not able to update contact details programatically in local phonebook - android

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));

Related

How do I update a email address from specific Email._id?

I have this problem .
I have a listview that is created depending on how many emails address have a contact .
Now I wish I could change this email address in a specific way .
If for example one of my contact has four email address ( one#gmail.com,
two#gmail.com,three#gmail.com,four#gmail.com )and modify one of these emails address for the second example ( two#yahoo.com ) how can I change this only leaving the other ?
For every email address I have a bean that includes : Email._id , Email.Type , Email.Data .
Thank you all for the help .
P.S.
Sorry for my English ;)
The problem is not in the listview , but in a specific point of contact email address .
This is the code that I used when I was a single email address to edit.
String emailData = "example#email.com";
String contactId="123456";
int typeEmail=1;
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE
+ "=?",
new String[] { contactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE })
.withValue(ContactsContract.CommonDataKinds.Email.DATA, emailData)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, typeEmail).build());
I solved it :
I simply changed the column to ContactsContract.Data.CONTACT_ID to ContactsContract.Data._ID
for (int i = 0; i < listEmail.size(); i++) {
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data._ID + "=? AND "
+ COLUMN_MIMETYPE + "=?",
new String[] { listEmail.get(i).getId(),
MIMETYPE_STRING_EMAIL })
.withValue(COLUMN_EMAIL, listEmail.get(i).getName())
.withValue(COLUMN_EMAIL_TYPE,
listEmail.get(i).getType()).build());
}

Update multiple phone numbers in contact

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.

Cannot update contact with new fields

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());
}
}

Update contact detail android

I am working on the an application in which I want to update contact of particular person. When I update only contact first and last name then it working fine but I want to update full detail of contact like email address, number, postal addres etc.
Please provide me some useful link. Thanks in advance.
each field (email, name, adress) has its on mime type, which you should use
in order to update the field.
lets try to update the email for instance.
First, you should find the detail you want to update.
we will work with Data table, where each Data.RAW_CONTACT_ID represents a detail
about some contact.
So, we need to find the Data.RAW_CONTACT_ID where the id is the id of the contact you want
to edit.
Now we need to find the mimetype (the specific row which represents the detail) of
email (Email.CONTENT_ITEM_TYPE).
The data of an email is stored in the column Email.DATA - there we put the new email.
if you want a specific email type, you should add it to the query:
for example, if you want to add a home-email, then you should add Email.TYPE_HOME
to the query.
then we build a query and finally apply the change.
Here's an examle:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
String emailParams = Data.RAW_CONTACT_ID + " = ? AND " + Data.MIMETYPE + " = ?";
String[] emailParamsWhere = new String[] { "contact_id", Email.CONTENT_ITEM_TYPE };
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI).withSelection(emailParams, emailParamsWhere).withValue(Email.DATA, "new email").withValue(Email.TYPE, Email.TYPE_HOME)
.build());
try
{
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
if (res != null)
{
return true;
}
return false;
}
catch (RemoteException e)
{
Log.d(TAG, e.getMessage());
e.printStackTrace();
}
catch (OperationApplicationException e)
{
Log.d(TAG, e.getMessage());
e.printStackTrace();
}
For updating mobile phone, use this query:
String phoneParams = Data.RAW_CONTACT_ID + " = ? AND " + Data.MIMETYPE + " = ? AND " + Phone.TYPE + " = " + Phone.TYPE_MOBILE;
String[] phoneParamsWhere = new String[] { "contact_id", Phone.CONTENT_ITEM_TYPE };
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI).withSelection(phoneParams, phoneParamsWhere).withValue(Phone.NUMBER, "mobile_number")
.withValue(Phone.TYPE, Phone.TYPE_MOBILE).build());
Hope I helped

Can't add a HOME email has been specified yet to an existing ANDROID contacs

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());

Categories

Resources