将联系人添加到ListView

时间:2010-08-18 17:37:30

标签: android

好吧,我正在学习如何使用联系人等。我希望将我的联系人列在ListView中,使用自定义适配器(我认为这就是所谓的,我可以在一个ListView条目中使用ImageViews,TextViews,所有这些)。我如何使用以下代码完成此操作?

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
while (cursor.moveToNext()) { 
    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
    String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
    if (Boolean.parseBoolean(hasPhone)) { 
        // You know have the number so now query it like this
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
        while (phones.moveToNext()) { 
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                 
        } 
        phones.close(); 
    } 
} 
cursor.close();

1 个答案:

答案 0 :(得分:1)

查看ContactManager样本代码。它解释了如何做你想要的: http://developer.android.com/resources/samples/ContactManager/index.html