Alberto Maluje
Alberto Maluje

Reputation: 126

adding custom type of number to a contact [Android]

I been working on an app that helps sending sms to your contacts. Everything is great, except that I'm supposed to add a "custom field number" to a contact such as "Work" or "Private". I'd search the web for answers and the ones that are useful for evryone, aren't for me. This is my code:

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

}

And it's called:

AddCtxtAttribute(contacto_id, contacto_numero);

where contacto_id and contacto_numero are a int and a String respectively.

The problem is that when I pressed the button, I have the ID (contacto_id) of that contact but it updates other contact. (like the id's don't match) but i've debug it and the id doesn't change.

Can anyone help me with this?

Upvotes: 3

Views: 2516

Answers (1)

Alberto Maluje
Alberto Maluje

Reputation: 126

I figured it out!

the problem was that i was using diferent column names and uris in reading the contacts and inserting a new contact number.

I did this: to query all contacts:

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

And to insert a new custom label:

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

Upvotes: 2

Related Questions