Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Device contact is not getting updated

$
0
0

Hi,

I have a Xamarin.Forms based application which uses operation with device contact like adding new contact, updating existing contact. Adding new contacts works, but update contact is not working. First i searched for that contact and get its' Id if it exist.
Below is my code

string LookupByPhone(string phoneNumber, string query = "")
    {
        var Id = "0";
        try
        {
            var context = MainActivity.Instance;

            var uri = ContactsContract.Contacts.ContentUri;
            uri = ContactsContract.CommonDataKinds.Phone.ContentUri;

            string[] projection = {
                ContactsContract.Contacts.InterfaceConsts.Id,

                InterfaceConsts.DisplayName,
                ContactsContract.CommonDataKinds.Phone.Number
                };

            var cursor = context.ContentResolver.Query(uri, projection, null, null, null);

            if (cursor.MoveToFirst())
            {
                do
                {
                    var id = cursor.GetString(cursor.GetColumnIndex(projection[0]));
                    var nm = cursor.GetString(cursor.GetColumnIndex(projection[1]));
                    var number = cursor.GetString(cursor.GetColumnIndex(projection[2]));

                    Id = id;
                    if (nm == query)
                        break;

                    //break;
                } while (cursor.MoveToNext());
            }
            cursor.Close();
        }
        catch (Exception ex)
        {
            LogController.LogError(ex);
        }
        return Id;
    }



void UpdateContact(string phonenumber, string id, string givenName, string familyName)
    {
        ContentProviderOperation.Builder builder = ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);


        List<ContentProviderOperation> ops = new List<ContentProviderOperation>();

        if (!string.IsNullOrWhiteSpace(givenName) && !string.IsNullOrWhiteSpace(familyName))
        {
            // Name
            String nameSelection = ContactsContract.Data.InterfaceConsts.ContactId + " = ? AND "
                                   + ContactsContract.Data.InterfaceConsts.Mimetype + " = ? ";
            String[] nameSelectionArgs = {
                                            id.ToString(),
                                            ContactsContract.CommonDataKinds.StructuredName.ContentItemType
            };


            builder.WithSelection(nameSelection, nameSelectionArgs);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.GivenName, givenName);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.FamilyName, familyName);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.DisplayName, givenName + " " + familyName);

            ops.Add(builder.Build());
        }

        #region Phone update
        // CellPhone
        String phoneSelection = ContactsContract.Data.InterfaceConsts.ContactId + " = ? AND " +
                                   ContactsContract.Data.InterfaceConsts.Mimetype + " = ? AND " +
                                   ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type + " = ?";

        String[] phoneselectionArgs = {
               id.ToString(),
                   ContactsContract.CommonDataKinds.Phone.ContentItemType,
               PhoneDataKind.Mobile.ToString()
            };

        builder = ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);
        builder.WithSelection(phoneSelection, phoneselectionArgs);

        builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, phonenumber);
        builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type, PhoneDataKind.Mobile.ToString());
        ops.Add(builder.Build());
        #endregion

        // Update the contact
        ContentProviderResult[] result;
        try
        {
        // the result return count{0}
        var result = MainActivity.Instance.ContentResolver.ApplyBatch(ContactsContract.Authority, ops);

        }
        catch (Exception ex)
        {
            LogController.LogError("Error updating phone:" + phonenumber,ex);
        }
    }

There are no error or exception. Any thought on this?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>