Search code examples
javaandroidcursorandroid-contentprovidersimplecursoradapter

Cursor with one phone number for each contact


I know how to get contact names and numbers in a cursor, but when I put them on a listview I get multiple numbers for the same contact in separate lines, i.e. if a contact has more than one number, all these numbers are displayed. How can I select only one number for each contact?

Uri uri = Phone.CONTENT_URI;
String[] projection = { Phone.DISPLAY_NAME, Phone.NUMBER, Phone._ID };
String sortOrder = Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

Cursor cursor = managedQuery(uri, projection, null, null, sortOrder);

Thanks for any help in advance!


Solution

  • If I got you right, and you want to show only one number per DISPLAY_NAME disregarding all others, you can use this hack:

    String selection = "1) GROUP BY (" + Phone.DISPLAY_NAME;
    Cursor cursor = managedQuery(uri, projection, selection , null, sortOrder);
    

    edit Short explanation: it's kind of sql-injection: it will insert 1 in WHERE part of the query, making it always true, and add GROUP BY, unsupported by managedQuery()