I can’t get the phone number out of a phone book

Asked

Viewed 105 times

0

I’m not getting the phone number to add to my list, someone knows?

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

1 answer

0


you need to loop because a contact can have several 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();
  • Dude, could you explain to me what each line does? I’m doing a survey and I don’t think so.. I wanted to understand the function of each line....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.