1

I'm trying to edit contact details programmatically. I want to change the contact name having the phone number is equal to 123. Here is my Non working code.

Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("123"));

        // This query will return NAME and ID of contact, associated with phone //number.

        Cursor mcursor = getContentResolver().query(lookupUri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);

        //Now retrive _ID from query result
        long idPhone = 0;
        try {
            if (mcursor != null) {
                if (mcursor.moveToFirst()) {
                    idPhone = Long.valueOf(mcursor.getString(mcursor.getColumnIndex(ContactsContract.PhoneLookup._ID)));
                    String getID = String.valueOf(idPhone);
                    Toast.makeText(this.getApplicationContext(), getID, Toast.LENGTH_LONG).show();
                    Uri uri= ContentUris.withAppendedId(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,3625);
                    ContentValues values = new ContentValues();
                    values.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,"After Changed name");
                    getContentResolver().update(uri, values, ContactsContract.CommonDataKinds.Phone._ID+"=?", new String[] {String.valueOf(idPhone)});

                }
            }
        } finally {
            mcursor.close();
        }

Currently I'm finding the phone number and getting the phone number's ID. With ID I'm trying to update it, seems it's not updating the phone number with ID.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Disha Shukla
  • 133
  • 1
  • 11

1 Answers1

0

here is working code for me.

 public void updateContact (String contactId, String newNumber, Activity act) throws RemoteException, OperationApplicationException{

    //ASSERT: @contactId alreay has a work phone number 
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
    String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + 
                    Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Phone.TYPE + "=?";
    String[] phoneArgs = new String[]{contactId, String.valueOf(Phone.TYPE_WORK)}; 
    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
            .withSelection(selectPhone, phoneArgs)
            .withValue(Phone.NUMBER, newNumber)
            .build()); 
    act.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}


 //hope this also work for you.
Vij
  • 445
  • 2
  • 9
  • I have phone number already. I want to change the contact name from given phone number. – Disha Shukla Aug 19 '16 at 06:26
  • pass contact number as parameter in contactId – Vij Aug 19 '16 at 06:33
  • ok disha it works for me you can check github example here. https://github.com/gwoodhouse/ContactContractSample/blob/master/ContactsIntegration/src/com/woodhouse/example/activity/ContactsIntegrationActivity.java – Vij Aug 19 '16 at 07:15
  • also you can check this answer http://stackoverflow.com/questions/9907751/android-update-a-contact – Vij Aug 19 '16 at 07:22
  • I didn't understand the difference between contact id and raw contact id. Can you please modify your code ? The link you gave me is hard to understand. – Disha Shukla Aug 19 '16 at 07:34
  • raw contact id returned when you insert a new contact into database and the you can refer http://stackoverflow.com/questions/9907751/android-update-a-contact – Vij Aug 19 '16 at 08:33