您的代码应该更新与您的WHERE
子句匹配的每一行。如果您知道有额外的行应该匹配但不匹配,那么那里有问题。
一种可能的测试方法:您可能首先要查询与ContactsContract.CommonDataKinds.Phone.NUMBER
给定匹配的事物ContactsContract.Data.RAW_CONTACT_ID
并查看返回了多少事物。
如有必要(尽管我不确定这与您已经在做什么不同),迭代您的结果while(cursor.moveToNext())
,如果电话号码与您尝试更新的电话号码匹配,update
它。
请注意,这是未经测试的现成代码,因此可能存在小错误,如果您真的这样做,您可能希望使用 aList<ContentProviderOperation>
然后执行批处理操作,而不是一个一个地执行此操作。
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));
}
UPDATE
如果您在语句中正确设置了所有内容,甚至不需要先执行查询。
您可能还会发现附加到此问题的代码块很有用。