locked
Updating a Contact Phone Number in Android RRS feed

  • Question

  • User281572 posted

    Hi,

    I am having a bit of trouble updating all a particular contact entry. At the moment I am able to edit the first and last name, but I am unable to edit the phone number or email without having some undesired consequences like renaming all the contacts on my phone including their number/email/name :-( I think that was due to my selection not clarifying the mimetype in the selection.

    At the moment I am have a cursor that gets the data id - ContactsContract.Data.InterfaceConsts.Id and I use this as the condition when selecting which contact to update. I wanted to use ContactsContract.Contacts.InterfaceConsts.Id, but this doesnt seem to let me access the phone or email or name (other than display name).

    I then use the data id as follows:

    `List ops = new List();

                String where = ContactsContract.Data.InterfaceConsts.Id + " = ? AND " +
                        ContactsContract.Data.InterfaceConsts.Mimetype + " = ? ";
    
                String[] p = new String[] {
                    _Contact.DataId.ToString(),
                    ContactsContract.CommonDataKinds.StructuredName.ContentItemType
                };
    
    
                ContentProviderOperation.Builder builder = ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);
                //builder.WithSelection(ContactsContract.CommonDataKinds.StructuredName.DisplayName + " LIKE '%" + _Contact.DisplayName + "%'", null);
                //builder.WithSelection(ContactsContract.Data.InterfaceConsts.Id + " LIKE '%" + _Contact.DataId + "%'", null);
                builder.WithSelection(where, p);
                builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.GivenName, _Contact.FirstName);
                builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.FamilyName, _Contact.LastName);
                ops.Add(builder.Build());
    
    
                String wherey = ContactsContract.Data.InterfaceConsts.Id + " = ? AND " +
                           ContactsContract.Data.InterfaceConsts.Mimetype + " = ? ";
    
                String[] py = new String[] { _Contact.DataId.ToString(),
                       ContactsContract.CommonDataKinds.Phone.ContentItemType
                };
    
    
    
                builder = ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);
                builder.WithSelection(wherey, py);
                builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, "999999999");
                ops.Add(builder.Build());
    
    
                // Update the contact
                ContentProviderResult[] result;
    
                try
                {
                    result = ContentResolver.ApplyBatch(ContactsContract.Authority, ops);
                    System.Diagnostics.Debug.WriteLine("result....................");
                }
                catch (Exception e) { System.Diagnostics.Debug.WriteLine("this is the error...................." + e.Message); }
    

    `

    This code lets me update the first and second name but does nothing to the phone number :-(

    I am completely lost at the moment, so any help would be greatly appreciated.

    Monday, December 5, 2016 5:13 PM

All replies

  • User74309 posted

    Have you got any success on this or find any resolution?

    Monday, July 31, 2017 9:06 PM