一个联系人下有多个号码时,仅更新电话的联系人号码 [英] Update only phone's contact number when multiple number under one Contact

查看:96
本文介绍了一个联系人下有多个号码时,仅更新电话的联系人号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像姓名="ABC"这样的移动联系人.电话 number ="123456789" type ="work" google number ="987654321" type =现在,当我当时更新编号为"123456789"的联系人时,首先获取该联系人的ID,然后使用phone.type ="work"更新该联系人.但是问题是,当我更新该联系人时,联系人将同时更新电话号码和Google号码等号码.那么我该如何更新电话的联系号码,而不更新与此ID关联的任何其他帐户?我编写的代码如下:

I have one contact in mobile like name="ABC".phone number="123456789" type="work" google number="987654321" type="work".Now when i update the contact of number "123456789" at that time first get the id of that contact and then update the contact with phone.type="work".But the problem is that when i update the contact then the contact will be update in both the number like phone number and google number.So how can i update only the phone's contact number but not any other account joined with this id?.I have written the code as below:

    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());} 

推荐答案

首先很难用上面的格式来阅读您的代码,这也许就是为什么没有尝试回答这个问题的原因,但是这是我用来解决的代码更新电话号码而不更新任何其他电话号码:

First of all its hard to read you code with the formatting you have above, this maybe why no tried to answer this, but here is the code I use to update a phone number without updating any other phone number:

    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.

The key is to have contact ID of the contact and phone ID of the phone you want to update in your selection for the query.

这篇关于一个联系人下有多个号码时,仅更新电话的联系人号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆