How can I update an existing Contact

Hi, I'm having trouble updating an existing contact for Xamarin.Android

Here is how I create a Contact:

    public void SaveContact()
    {
        List<ContentProviderOperation> ops = new List<ContentProviderOperation>();
        int rawContactInsertIndex = ops.Count;

        ops.Add(ContentProviderOperation.NewInsert(Android.Provider.ContactsContract.RawContacts.ContentUri)
            .WithValue(Android.Provider.ContactsContract.RawContacts.InterfaceConsts.AccountType, null)
            .WithValue(Android.Provider.ContactsContract.RawContacts.InterfaceConsts.AccountName, null).Build());
        ops.Add(ContentProviderOperation
            .NewInsert(Android.Provider.ContactsContract.Data.ContentUri)
            .WithValueBackReference(Android.Provider.ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex)
            .WithValue(Android.Provider.ContactsContract.Data.InterfaceConsts.Mimetype, Android.Provider.ContactsContract.CommonDataKinds.StructuredName.ContentItemType)
            .WithValue(Android.Provider.ContactsContract.CommonDataKinds.StructuredName.DisplayName, "FirstName LastName") // Name of the person
            .Build());
        ops.Add(ContentProviderOperation
            .NewInsert(Android.Provider.ContactsContract.Data.ContentUri)
            .WithValueBackReference(
                ContactsContract.Data.InterfaceConsts.RawContactId, rawContactInsertIndex)
            .WithValue(Android.Provider.ContactsContract.Data.InterfaceConsts.Mimetype, Android.Provider.ContactsContract.CommonDataKinds.Phone.ContentItemType)
            .WithValue(Android.Provider.ContactsContract.CommonDataKinds.Phone.Number, "9999999999") // Number of the person
            .WithValue(Android.Provider.ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type, "mobile").Build()); // Type of mobile number  

        // Asking the Contact provider to create a new contact                 
        try
        {

            Android.App.Application.Context.ContentResolver.ApplyBatch(ContactsContract.Authority, ops);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }

Here is how I am trying to update a contact
No exceptions happen, but nothing is being updated

    public void UpdateContact()
    {
       List<ContentProviderOperation> ops = new List<ContentProviderOperation>();

       String orgWhere = ContactsContract.Data.InterfaceConsts.ContactId + " = ? AND " + ContactsContract.Data.InterfaceConsts.Mimetype + " = ?";
       String[] orgWhereParams = new String[]
        {
            "3", //Pretty sure this is the ID of the contact
            ContactsContract.CommonDataKinds.Organization.ContentItemType
        };
         ops.Add(ContentProviderOperation
        .NewUpdate(ContactsContract.Data.ContentUri)
        .WithSelection(orgWhere, orgWhereParams)
        .WithValue(ContactsContract.CommonDataKinds.Phone.Number, "0123456789")
        .Build());

        // Asking the Contact provider to create a new contact                 
        try
        {

            Android.App.Application.Context.ContentResolver.ApplyBatch(ContactsContract.Authority, ops);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }


    }

Can someone point me in the right direction? I can't find anything about updating contacts for Xamarin Android

Answers

Sign In or Register to comment.