tags:

views:

33

answers:

1

I am learning android. I am trying to upadate contact number programmatically. Could anyone help me please how can I do that.

My effort is:

String lNumber = pCur.getString( pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));   

ContentValues values = new ContentValues();

Uri lPhoneUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, ContactsContract.CommonDataKinds.Phone.NUMBER);                      

values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "45323333"));
getContentResover().update(lPhoneUri, values, ContactsContract.CommonDataKinds.Phone.NUMBER+"=?", new String[] { lNumber });        
A: 

I think you are pretty much there. The following uses the new API to update the WORK phone number of a contact, assume that that contact already has a work phone number.

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);
}
hungh3