Android - Getting Contact List with street addresses but no low value ones like Skype where the address is only a city and state
Asked Answered
C

1

7

I got a cursor retrieving all the contacts on the app that have a street address. This cursor is then passed into an Adapter. So far so good. Except I also get a bunch of low value contacts (mostly from Skype) that only have a State/Country info. Is there an easy way to modify the URI to skip those?

public Cursor getDirectoryList (CharSequence constraint)  {

        String[] selectionArguments = { "%"+constraint.toString()+"%" };
        String selection = ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME + " like ?";

        Uri uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
        String sortOrder = ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
        Cursor cr = getContentResolver().query(uri, null, selection, selectionArguments, sortOrder);

        return cr;
    }
Complex answered 28/4, 2016 at 2:3 Comment(0)
B
4

You can modify the selection and/or selectionArgs to specify more criteria, such as ensuring that the Street field is not null:

String selection =
    ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME + " like ? AND " +
    ContactsContract.CommonDataKinds.StructuredPostal.STREET + " IS NOT NULL";

This is just SQL, so specify as many fields from ContactsContract.CommonDataKinds.StructuredPostal, and whatever conditions on them, that you want.

Blether answered 6/5, 2016 at 18:22 Comment(1)
Perfect. Worked like a charm.Complex

© 2022 - 2024 — McMap. All rights reserved.