-1

I want to retrieve all contact names and numbers from a mobile phone in Android. Then, I want to display that data in a ListView. How can I achieve this?

stealthjong
  • 10,858
  • 13
  • 45
  • 84

2 Answers2

0

try this...

  public void getContacts(ContentResolver cr) {
    Cursor phones = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    while (phones.moveToNext()) {
        String name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

    }
    phones.close();// close cursor

}

call method

 getContacts(this.getContentResolver());
Rakesh Rangani
  • 1,039
  • 10
  • 13
0

from this code you will get name n numbers save in your contact list, add them in arraylist or array, n then you may show them in listview

try {

            String phoneNumber = null;

            Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;

            String _ID = ContactsContract.Contacts._ID;

            String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;

            String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

            Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

            String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;

            String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

            ContentResolver contentResolver = getContentResolver();

            Cursor cursor = contentResolver.query(CONTENT_URI, null, null,
                    null, null);

            if (cursor.getCount() > 0) {

                while (cursor.moveToNext()) {

                    String contact_id = cursor.getString(cursor
                            .getColumnIndex(_ID));

                    int hasPhoneNumber = Integer
                            .parseInt(cursor.getString(cursor
                                    .getColumnIndex(HAS_PHONE_NUMBER)));

                    if (hasPhoneNumber > 0) {

                        Cursor phoneCursor = contentResolver.query(
                                PhoneCONTENT_URI, null, Phone_CONTACT_ID
                                        + " = ?", new String[] { contact_id },
                                null);

                        while (phoneCursor.moveToNext()) {

                            phoneNumber = phoneCursor.getString(phoneCursor
                                    .getColumnIndex(NUMBER));



                                String contname = phoneCursor.getString(phoneCursor
                                        .getColumnIndex(DISPLAY_NAME));

                                if (!contname.equals(null)) {


                                    Toast.makeText(getApplicationContext(),
                                            contname, Toast.LENGTH_SHORT).show();

                                }

                            }

                        }
                        phoneCursor.close();

                    }

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
nawaab saab
  • 1,892
  • 2
  • 20
  • 36