2012-09-07 147 views
2

我有一个联系人在移动像名称=“ABC”。 phone number =“123456789”type =“work”google number =“987654321”type =“work”。现在,当我更新号码“123456789”的联系人时,首先获取该联系人的ID,然后更新与phone.type =“工作”的联系人。但问题是,当我更新的联系人,然后联系人将更新的数字,如电话号码和谷歌number.So如何更新只有手机的联系号码,但没有任何其他帐户与此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());} 
+0

您是否找到了解决方案?如果你发现你可以发布它?感谢 – nikmin

回答

0

首先它很难看到你与你有上面的格式代码,这可能是为什么没有试图回答这个问题,但这里是我用来更新没有upd的电话号码的代码阿婷任何其他电话号码:

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。

+0

你能否提供用于获取特定联系人的联系人ID和电话ID的URI? –

相关问题