I can not pick up the phone number from a phone book on the phone

0

I can not get the phone number to add to my list, does anyone know?

public void pegarContatos() {

        Uri agenda = ContactsContract.Contacts.CONTENT_URI;
        Cursor cursor = getContentResolver().query(agenda, null, null, null, null);

        while (cursor.moveToNext()) {

            int telefone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

            if(telefone == 1) {
                int ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
                String nome = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                Contato contato = new Contato(ID ,nome);

                listaDeContatos.add(contato);
            }
        }

        this.lista();

        cursor.close();
    }
    
asked by anonymous 05.12.2016 / 02:47

1 answer

0

Looping is required because a contact can have multiple numbers.

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
    int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
    String nome = cursor.getString(nameFieldColumnIndex);

    String ID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

    ArrayList numeros = new ArrayList();

    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + ID, null, null);
    while (phones.moveToNext()) {
        String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        numeros.add(number);
    }
    phones.close();
}
cursor.close();
    
05.12.2016 / 10:44