如何在Android中获取所有联系人姓名和号码?

时间:2014-08-28 10:02:42

标签: android listview android-listview

我想从Android手机中检索所有联系人姓名和号码。然后,我想在ListView中显示该数据。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

试试这个......

  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

}

通话方法

 getContacts(this.getContentResolver());

答案 1 :(得分:0)

从这段代码中你将获得名称n号码保存在联系人列表中,将它们添加到arraylist或数组中,然后你可以在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();
        }