I need to get contacts information(cursor) with email. They must be distinct. There must be one entry per contact if he has got an email. How to do it? I am targetting new c
Try using this snippet: Showing Contact name and Email on the same row in list view.
/**
* Populate the contact list based on account.
*/
private void populateContactList() {
// Build adapter with contact entries
Cursor cursorEmail = getContactsEmail();//get all emails
String[] fields = new String[] //fields of data to take
{ ContactsContract.Contacts._ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, R.layout.contact_entry, cursorEmail ,
fields, new int[]
{R.id.UID,R.id.contactEntryText,R.id.contactEmail});
mContactList.setAdapter(adapter);
}
/**
* Obtains the contact list for the currently selected account.
*
* @return A cursor for for accessing the contact list.
*/
private Cursor getContactsEmail()
{
// Run query
Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP +"='1'";
//showing only visible contacts
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}