Chiggins
Chiggins

Reputation: 8397

Adding contacts to a ListView

Alright so I'm just learning how to work with contacts and such. I would like to have my contacts listed out in a ListView, with custom adapter (I think thats what it is called, where I can have ImageViews, TextViews, all that such, in one ListView entry). How would I be able to do that with the following code that I have?

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

Upvotes: 0

Views: 485

Answers (1)

Check out ContactManager samplecode. It explains exactly how to do what you are looking for: http://developer.android.com/resources/samples/ContactManager/index.html

Upvotes: 1

Related Questions