Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit existing Contacts in Android?

I am trying to edit existing phone contacts by using application. By using contact name am getting the corresponding contact id.Then am trying to update the existing contacts by using the contact id. But unfortunately it is not updating.

My code is as follows,

 String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" + edt_nameDetail.getText() + "\" )";
                    Cursor c = getActivity().getApplicationContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
                    getActivity().startManagingCursor(c);

                    if (c.moveToNext()) {
                         ContactId = c.getString(0);
                        Log.e("Tag contact id ","edit contact id "+ ContactId);
                    }


                    try
                    {
                       String name = edt_nameDetail.getText().toString().trim();
                       String email = edt_contactEmailDetail.getText().toString().trim();
                       String number = edt_mobileNumberDetail.getText().toString().trim();


                            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(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
                                        .withSelection(where,nameParams)
                                        .withValue(ContactsContract.CommonDataKinds.StructuredName.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());
                                contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
                                Toast.makeText(getActivity(), "Contact is successfully edited", Toast.LENGTH_SHORT).show();

                            }
                            else {
                                Toast.makeText(getActivity(), "Fail edit", Toast.LENGTH_SHORT).show();
                            }

1 Answers

If you are searching with email then update your .withSelection as

.withSelection(ContactsContract.CommonDataKinds.Email.ADDRESS + "=? AND " +
               Data.MIMETYPE + "='" +
               ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE + "'",
               new String[]{email})

update all your withselection code corresponding to your search type

like image 104
NIPHIN Avatar answered Nov 22 '25 07:11

NIPHIN