Friday, May 6, 2016

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

Leave a Comment

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;     } 

1 Answers

Answers 1

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.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment