user4928488
user4928488

Reputation:

Modify the existing contacts in Android

I am try to edit phone contacts through my app. I want to edit "Name , Number, Email ". I am able to edit Number and Email. But when I try to edit Name it is not editing

My code as follows

ContentResolver contentResolver  = getActivity().getContentResolver();

String where = ContactsContract.Data.CONTACT_ID + " = ? AND " +  ContactsContract.Data.MIMETYPE + " = ?";

 String[] emailParams = new String[]{ContactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
 String[] nameParams = new String[]{ContactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
 String[] numberParams = new String[]{ContactId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};

 ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<android.content.ContentProviderOperation>();

 if(!email.equals("") &&!name.equals("")&& !number.equals(""))
  {
   ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
        .withSelection(where,emailParams)
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
        .build());

   ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
        .withSelection(ContactsContract.CommonDataKinds.Phone._ID + " = ?", new String[] {ContactId})
        .withValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, name)
        .build());

     ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
        .withSelection(where,numberParams)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
        .build());

    getActivity().getApplicationContext().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    Toast.makeText(getActivity(), "Contact is successfully edited", Toast.LENGTH_SHORT).show();
  }

Can any please tell me why name is not editing

Thanks in advance :)

Upvotes: 3

Views: 815

Answers (1)

RocketRandom
RocketRandom

Reputation: 1122

You selection is incorrect

withSelection(ContactsContract.CommonDataKinds.Phone._ID + " = ?", new String[] {ContactId})

Phone._ID is not contactId - its the Data._ID.

You have the nameParams defined correctly but for some reason I see you have not used them.

use below instead

.withSelection(where,nameParams )

Use the same selection that you used for number and email.

Upvotes: 1

Related Questions