Aravin
Aravin

Reputation: 4108

Contact details update not possible

Following code is to update the contact. i am able to update the phone number field but i am trying to update the contact name (inside the comment line) but it is not possible i cant understand the code can anyone help me to solve this.

String new_phoneNumber = update_phonenumber.getText()
                    .toString();
String new_name = update_name.getText().toString();
ContentResolver cr = getContentResolver();

String where = ContactsContract.Data.DISPLAY_NAME
                    + " = ? AND "
                    + ContactsContract.Data.MIMETYPE
                    + " = ? AND "
                    + String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE)
                    + " = ? ";
String[] params = new String[] {
                    get_name,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
                    String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) };

 Cursor phoneCur = managedQuery(
                    ContactsContract.Data.CONTENT_URI, null, where, params,
                    null);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

            if ((null == phoneCur)) {
                Toast.makeText(getApplicationContext(), "Empty Contact",
                        Toast.LENGTH_LONG).show();
            } else {
                ops.add(ContentProviderOperation
                        .newUpdate(ContactsContract.Data.CONTENT_URI)
                        .withSelection(where, params)
            .withValue(
                                ContactsContract.CommonDataKinds.Phone.DATA,
                                new_phoneNumber)/***.withValue(
                                ContactsContract.CommonDataKinds.Phone.DATA,
                                new_name)***/
                        .build());
            }

            phoneCur.close();

            try {
                cr.applyBatch(ContactsContract.AUTHORITY, ops);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OperationApplicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Toast.makeText(getApplicationContext(), "Successfully updated",
                    Toast.LENGTH_LONG).show();

Upvotes: 2

Views: 113

Answers (2)

Arut
Arut

Reputation: 950

You can add another ops.add in your code and insert the following code.

ops.add(ContentProviderOperation
                    .newUpdate(ContactsContract.Data.CONTENT_URI)
                    .withSelection(Data.DATA1 + "=?", new String[] {get_name})
                    .withValue(
                            StructuredName.DISPLAY_NAME,
                            new_name)
                    .build());

Upvotes: 2

Jay Gajjar
Jay Gajjar

Reputation: 2741

Reffer to this link it will explain how ContactContract works

http://developer.android.com/guide/topics/providers/contacts-provider.html

Each field (email, name, address) has its on mime type, which you should use in order to update the field.

lets try to update the email for instance.

First, you should find the detail you want to update. we will work with Data table, where each Data.RAW_CONTACT_ID represents a detail about some contact.

So, we need to find the Data.RAW_CONTACT_ID where the id is the id of the contact you want to edit.

Now we need to find the mime-type (the specific row which represents the detail) of email (Email.CONTENT_ITEM_TYPE).

The data of an email is stored in the column Email.DATA - there we put the new email. Then we build a query and finally apply the change.

Upvotes: 0

Related Questions