2

当其中一个联系人有多个号码时,例如:

显示名称:GuessWho
TYPE = home
Number = homeNumber
TYPE = mobile
Number = mobileNumber
TYPE = other
Number = otherNumber ...

总之......其中之一

如何从此联系人中删除带有号码的类型(比如说“移动”)?我必须使用从上一个查询中获取的用户 ID 来更新它,或者如何?我只需要删除一个带有数字的 TYPE,联系人的其他字段必须保持不变。

我正在使用这段代码来获取联系:

int indexName = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexNumber = c
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    int indexType = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
    int indexID = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);

String name = c.getString(indexName);
        String number = c.getString(indexNumber);
        String type = c.getString(indexType); 
        String typeStored = (String) Phone.getTypeLabel(mContext.getResources(), Integer.parseInt(type), "");
        Log.i("TYPE READED : ", typeStored);
        String id = c.getString(indexID);

其中 c 是查询的光标。

4

1 回答 1

1

来自同一联系人的每个号码都有自己的 ID。你应该用它来删除。但是您还需要联系 ID。您可以使用以下代码行获取它:

contactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

然后使用以下代码删除该号码:

Cursor cur = contentResolver.query(RawContacts.CONTENT_URI, 
            new String[]{RawContacts._ID},
            RawContacts.CONTACT_ID + "=?", 
            new String[] {contactID.toString()}, null);
    int rowId=0;;
    if(cur.moveToFirst()){
        rowId = cur.getInt(cur.getColumnIndex(RawContacts._ID));
    }

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    String selectPhone = Data.RAW_CONTACT_ID + " = ? AND " + 
                         Data.MIMETYPE + " = ? AND " + 
                         Phone._ID + " = ?";
    String[] phoneArgs = new String[] { Integer.toString(rowId), 
                                        Phone.CONTENT_ITEM_TYPE, 
                                        ID.toString()};

    ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
            .withSelection(selectPhone, phoneArgs).build());
    try {
        contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        e.printStackTrace();
    }
于 2013-07-25T12:29:06.250 回答