2011-12-03 103 views
2

我一直致力於幫助向您的聯繫人發送短信的應用。一切都很好,除了我應該爲「聯繫人」或「私人」之類的聯繫人添加「自定義字段號碼」。我會在網上尋找答案,而那些對伊夫琳很有用的答案,並不適合我。這是我的代碼:將自定義類型的號碼添加到聯繫人[Android]

private void AddCtxtAttribute(int contactID, String contactNumber) { 

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 

    ContentProviderOperation.Builder builder = ContentProviderOperation 
      .newInsert(Data.CONTENT_URI); 

    builder.withValue(Data.RAW_CONTACT_ID, contactID); 
    builder.withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); 
    builder.withValue(ContactsContract.Data.DATA1, contactNumber); 
    builder.withValue(ContactsContract.Data.DATA2, Phone.TYPE_CUSTOM); 
    builder.withValue(ContactsContract.Data.DATA3, "My Custom Label"); 

    ops.add(builder.build()); 

    try { 
     getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
     Log.e("RESULT", "Success! " + contactID + " - " 
       + contactNumber); 
    } catch (RemoteException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Log.e("ERROR", "error : " + e.toString()); 
    } catch (OperationApplicationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Log.e("ERROR", "error : " + e.toString()); 
    } 

} 

,這就是所謂:

AddCtxtAttribute(contacto_id, contacto_numero); 

其中contacto_id和contacto_numero是一個int和分別的字符串。

問題是,當我按下按鈕時,我有該聯繫人的ID(contacto_id),但它更新其他聯繫人。 (如ID不匹配),但我已經調試它,ID不會改變。

任何人都可以幫助我嗎?

回答

2

我想通了!

問題是我在閱讀聯繫人和插入新聯繫人號碼時使用了不同的列名和uris。

我這樣做: 查詢所有聯繫人:

private void llenar_contactos() { 

    ProgressDialog progress; 
    lista_contactos_cel_sobrantes = null; 

    sobrantes = false; 

    lista_contactos_cel = new ArrayList<HashMap<String, Object>>(); 

    progress = ProgressDialog.show(contactsActivity.this, "", 
      "Cargando Contactos. Porfavor espere..."); 

    String[] projection = new String[] { Data.RAW_CONTACT_ID, 
      Phone.DISPLAY_NAME, Phone.NUMBER, Phone.LABEL }; 

    String selection = Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"; 

    // ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "='1'"; 
    String sort_order = "display_name ASC"; 

    Uri mContacts = Data.CONTENT_URI; 
    // ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 

    Cursor contacts = getContentResolver().query(mContacts, // Contact URI 
      projection, // Which columns to return 
      selection, // Which rows to return 
      null, // Where clause parameters 
      sort_order // Order by clause 
      ); 

    total_contactos = contacts.getCount(); 

    if (total_contactos > 0) { 

     int i = 0; 

     int multiplo = 0; 
     int indice_total = 0; 

     int temp_registros = 0; 

     String contact_id = ""; 
     String name = ""; 
     String phoneNo = ""; 
     String label = ""; 
     sobrantes = false; 

     int nameFieldColumnIndex = 0; 

     while (contacts.moveToNext()) { 
      nameFieldColumnIndex = contacts.getColumnIndex(Data.RAW_CONTACT_ID); 

      if (nameFieldColumnIndex > -1) { 
       contact_id = contacts.getString(nameFieldColumnIndex); 
      } 

      nameFieldColumnIndex = contacts.getColumnIndex(Phone.DISPLAY_NAME); 

      if (nameFieldColumnIndex > -1) { 
       name = contacts.getString(nameFieldColumnIndex); 
      } 

      nameFieldColumnIndex = contacts.getColumnIndex(Phone.NUMBER); 

      if (nameFieldColumnIndex > -1) { 
       phoneNo = contacts.getString(nameFieldColumnIndex); 
      } 

      nameFieldColumnIndex = contacts.getColumnIndex(Phone.LABEL); 

      if (nameFieldColumnIndex > -1) { 
       label = contacts.getString(nameFieldColumnIndex); 
      } 

      if(label != null){ 
       Log.i("CONTACTO", "id: " + contact_id + " -> name: " + name 
         + " ->number: " + phoneNo + " ->label: " + label); 
      } else { 
       Log.d("CONTACTO", "id: " + contact_id + " -> name: " + name 
         + " ->number: " + phoneNo + " ->label: " + label); 
      }    


     } 
     contacts.close(); 
    } 
} 

並插入一個新的自定義標籤:

private void AddCtxtAttribute(int contactID, String contactNumber) { 

    if (contactID != 0 && contactNumber != null) { 

     ArrayList<ContentProviderOperation> ops = 
       new ArrayList<ContentProviderOperation>(); 

    ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) 
       .withValue(Data.RAW_CONTACT_ID, contactID) 
       .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) 
       .withValue(Phone.NUMBER, contactNumber) 
       .withValue(Phone.TYPE, Phone.TYPE_CUSTOM) 
       .withValue(Phone.LABEL, "My Custom Label") 
       .build()); 
    try{ 
     getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);   
    } catch (RemoteException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (OperationApplicationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }   
    } 
} 
相關問題