当一个联系人下有多个号码时,仅更新电话的联系号码

时间:2012-09-07 13:48:56

标签: android contacts

我在移动设备上有一个联系人,例如name =“ABC”。电话 number =“123456789”type =“work” google number =“987654321”type =“当我更新号码“123456789”的联系人时,首先获取该联系人的ID,然后用phone.type =“work”更新联系人。但问题是,当我更新联系人然后联系人将更新电话号码和谷歌号码等号码。那么我怎样才能更新手机的联系电话号码,而不更新任何其他加入此ID的帐号?。我已编写如下代码:

    public Long getID(String number) {

            Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(number));
            Cursor c = getContentResolver().query(uri,
                    new String[] { PhoneLookup._ID}, null, null, null);
            while (c.moveToNext()) {
                return c.getLong(c.getColumnIndex(PhoneLookup._ID));
            }
            return null;
        }  
 public int gettype(String number) {

            Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(number));
            Cursor c = getContentResolver().query(uri,
                    new String[] { PhoneLookup.TYPE }, null, null, null);
            while (c.moveToNext()) {
            return c.getInt(c.getColumnIndex(PhoneLookup.TYPE));

            }
            return 0;
        }


Long id = getID(delnumber);
int contact_type= gettype(delnumber);


 String selectPhone = Data.CONTACT_ID+ "=? AND "    + Data.MIMETYPE+ "='"+ Phone.CONTENT_ITEM_TYPE+ "'" + " AND " + Phone.TYPE + "=?";
                                                    Log.i("type",""+contact_type);
                                                    if(contact_type==1)
                                                    {String[] phoneArgs = new String[] {String.valueOf(id), String.valueOf(Phone.TYPE_HOME)};
                                                    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI).withSelection(selectPhone,phoneArgs).withValue(Phone.NUMBER,getnum).build());}
                                                    else if(contact_type==2)
                                                    {String[] phoneArgs = new String[] {String.valueOf(id), String.valueOf(Phone.TYPE_MOBILE)};
                                                    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI).withSelection(selectPhone,phoneArgs).withValue(Phone.NUMBER,getnum).build());}
                                                    else if(contact_type==3)
                                                    {String[] phoneArgs = new String[] {String.valueOf(id), String.valueOf(Phone.TYPE_WORK)};
                                                    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI).withSelection(selectPhone,phoneArgs).withValue(Phone.NUMBER,getnum).build());}
                                                    else
                                                    {String[] phoneArgs = new String[] {String.valueOf(id), String.valueOf(Phone.TYPE_MOBILE)};
                                                    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI).withSelection(selectPhone,phoneArgs).withValue(Phone.NUMBER,getnum).build());} 

1 个答案:

答案 0 :(得分:0)

首先,很难用上面的格式读取你的代码,这可能是为什么没有试图回答这个,但这里是我用来更新电话号码而不更新任何其他电话号码的代码:

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(ContactsContract.Data.CONTACT_ID + " = ?", new String[] {userId})
            .withSelection(ContactsContract.Data._ID + " = ?", new String[] {phoneId})
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.Data.DATA1, phoneNumber)
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, type)
            .withValue(ContactsContract.CommonDataKinds.Phone.LABEL, label)
            .build());

    try 
    {
        resolver.applyBatch(ContactsContract.AUTHORITY, ops);
    } 

关键是要在您选择的查询中更新要更新的手机的联系人ID和手机ID。

相关问题