How to display phone contacts only (exclude SIM co

2019-02-27 12:37发布

问题:

I'm building an UI where I need to show a list of phone contacts in a list view.

I'm using ContactsContract.Data and the CursorLoader to load the data and then binding the cursor to a custom adapter (extended off of SimpleCursorAdapter).

The issue here is that I can't figure out how to filter out the SIM contacts; a test phone I have has identical contacts on the phone as well as the SIM, which causes the listview to have duplicate entries. If I remove the SIM, the duplicates go away.

How can I make this filter out SIM contacts? I'm looking for a way to get the data using 1 query.

This is how I load my data at the moment:

Uri queryUri = ContactsContract.Data.CONTENT_URI;

String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Data.MIMETYPE,
        ContactsContract.RawContacts.ACCOUNT_TYPE };

selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = 1  AND IS_PRIMARY = 1 AND MIMETYPE = '" + Phone.CONTENT_ITEM_TYPE + "'";


cursorLoader = new CursorLoader(getActivity(), queryUri, projection, selection, null, ContactsContract.Contacts.DISPLAY_NAME);

cursor = cursorLoader.loadInBackground();

//setup adapter, bind to listview etc..

回答1:

Figured it out:

Basically what you need is:

Uri queryUri = ContactsContract.Contacts.CONTENT_URI;

This would obey the user's address book setting - if the user has disabled showing SIM contacts, the query results would automatically exclude SIM contacts.

Hope this helps someone else.