How to display phone contacts only (exclude SIM contacts)

二次信任 提交于 2020-01-21 15:18:26

问题


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.



来源:https://stackoverflow.com/questions/16651609/how-to-display-phone-contacts-only-exclude-sim-contacts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!