向联系人添加自定义数字类型[Android]

时间:2011-12-03 21:41:42

标签: android android-contacts

我一直致力于帮助向您的联系人发送短信的应用。一切都很棒,除了我应该向“工作”或“私人”等联系人添加“自定义字段编号”。我会在网上搜索答案,那些对evryone有用的东西不适合我。这是我的代码:

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和String。

问题在于,当我按下按钮时,我有该联系人的ID(contacto_id),但它会更新其他联系人。 (就像id不匹配)但我调试它并且id不会改变。

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分: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();
        }           
    }
}