-1

我需要为同一个联系人更新多个电话号码。我只能更新一个电话号码,但我不知道如何更新例如 2

这是我的代码...

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); 
4

1 回答 1

1

您的代码应该更新与您的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如果您在语句中正确设置了所有内容,甚至不需要先执行查询。

您可能还会发现附加到此问题的代码块很有用。

于 2012-08-20T15:14:13.817 回答