Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
    }
like image 876
Pyro979 Avatar asked Apr 28 '16 02:04

Pyro979


1 Answers

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.

like image 93
Jeremy Dowdall Avatar answered Oct 20 '22 09:10

Jeremy Dowdall