当其中一个联系人有多个号码时,例如:
显示名称:GuessWho
TYPE = home
Number = homeNumber
TYPE = mobile
Number = mobileNumber
TYPE =其他
Number = otherNumber ...
总结......来自those。
如何从此联系人中删除带有号码的TYPE(让我们说“移动”)?我必须使用从上一个查询中获取的userID更新它,或者如何? 我只需要删除一个带有数字的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是查询的光标。
答案 0 :(得分: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();
}