2012-09-07 146 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? –

相關問題