2010-08-18 122 views
0

好吧,我只是在学习如何使用联系人等。我希望我的联系人在ListView中列出,并使用自定义适配器(我认为这就是所谓的,我可以在一个ListView条目中拥有ImageViews,TextViews等等)。我将如何用下面的代码来实现这一点?将联系人添加到ListView

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

查看ContactManager示例代码。它解释了如何做你正在寻找什么: http://developer.android.com/resources/samples/ContactManager/index.html

+0

我这样做,但它不会显示任何东西,当我运行该应用程序。 http://chiggins.pastebin.com/Hi4X58KX – Chiggins 2010-08-19 22:56:03

+0

可否详细说明一下。你看到logcat中的任何错误?你在运行样本吗? – 2010-08-22 00:14:21