【问题标题】:Delete a single phone type number from a contact从联系人中删除单个电话类型号码
【发布时间】:2013-07-25 05:52:08
【问题描述】:

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

显示名称:GuessWho
TYPE = home
Number = homeNumber
TYPE = 手机
Number = mobileNumber
TYPE = 其他
number = oofynumber ...

总之...来自those

如何从该联系人中删除带有号码的 TYPE(比如说“手机”)?我必须使用从上一个查询中获取的用户 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 是查询的光标。

【问题讨论】:

    标签: android cursor android-contacts phone-number


    【解决方案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();
        }
    

    【讨论】:

    • 我想“contentResolver”来自“myContext.getContentResolver()”,其中 myContext 是上下文?
    • 是的。这是我的代码的一部分,我没有更改表示法:) 我希望答案有帮助
    • 谢谢,它工作得很好......如果我想更新而不是删除具有给定 ID 的数字?
    • ArrayList&lt;ContentProviderOperation&gt; ops = new ArrayList&lt;ContentProviderOperation&gt;(); String selectPhone = Data._ID + " = ?"; String[] phoneArgs = new String[] { ID.toString() }; ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) .withSelection(selectPhone, phoneArgs) .withValue(Phone.NUMBER, phoneNo) .build()); try { contentResolver.applyBatch(ContactsContract.AUTHORITY, ops); } catch (RemoteException e) { e.printStackTrace(); } catch (OperationApplicationException e) { e.printStackTrace(); }这只是为了更新号码
    • mmh 它在我的应用程序中不起作用...“ID.toString()”是要更新的号码 ID,而 phoneNo 是新号码?
    猜你喜欢
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    相关资源
    最近更新 更多