-1

I need to update more than one phone number for the same contact. I can update only one phone number, but I don't know how I can update 2 for example

Here is my code...

String stEtPhone = etPhone.getText().toString();

values.clear();
String phoneWhere = ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?";
String[] phoneWhereParams = new String[]{String.valueOf(idContacto),ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,stEtPhone);
cr.update(ContactsContract.Data.CONTENT_URI, values, phoneWhere, phoneWhereParams); 
Kara
  • 6,115
  • 16
  • 50
  • 57
Ricardo Filipe
  • 435
  • 4
  • 8
  • 24

1 Answers1

1

Your code should update every row that matches your WHERE clause. If you know there are extra rows that should be matching but aren't, then something is wrong there.

One possible way to test: You might first want to query for things that match ContactsContract.CommonDataKinds.Phone.NUMBER for the given ContactsContract.Data.RAW_CONTACT_ID and see how many things are returned.

If necessary (though I'm not sure how precisely this is different than what you're already doing), iterate over your results while(cursor.moveToNext()) and if the phone number matches the one you're trying to update, update it.

Note that this is untested, off-the-cuff code, so there are probably small bugs, and that if you were doing this for real you would want to use a List<ContentProviderOperation> and then do a batch operation, instead of doing this one by one.

ContentResolver resolver = getContentResolver();
String[] columnsToReturn = {Data._ID, ContactsContract.CommonDataKinds.Phone.NUMBER}
// This should return all of the phone numbers that match your criteria
Cursor cursor = resolver.query(ContactsContract.Data.CONTENT_URI, columnsToReturn, phoneWhere, phoneWhereParams, null);

while(cursor != null && cursor.moveToNext()){
    // This code doesn't do this, but you might want to see how many items 
    // are returned in your cursor here to verify that you're getting what 
    // you should for some known case..

    // I'm using the row ID here (Data._ID) to update, instead of your initial WHERE
    resolver.update(ContactsContract.Data.CONTENT_URI, values, Data._ID + "=?", cursor.getString(cursor.getColumnId(Data._ID));

}

It should not even be necessary to perform the query first, if you get everything set up correctly in your UPDATE statement.

You may also find the chunk of code attached to this question useful.

Community
  • 1
  • 1
Jon O
  • 6,532
  • 1
  • 46
  • 57