I have a problem in querying phonebook contacts. What I need to do is get a list of contacts that have both phone and email entered or are of a specific type.
Basically like this:
public static final String SELECTION =
"("+ContactsContract.Contacts.HAS_PHONE_NUMBER +"='1') OR " + RawContacts.ACCOUNT_TYPE + "='" + Constants.ACCOUNT_TYPE + "'";
Now, the problem is, that RawContacts.ACCOUNT_TYPE does not exist in the ContactsContract.Contacts.CONTENT_URI, which I use with my query. I’m guessing I’d need to join another table, but have no idea how to do so.
Can anyone help me here, please?
The best way to read a raw contact along with all the data associated with it is by using the
ContactsContract.RawContacts.Entitydirectory. If the raw contact has data rows, the Entity cursor will contain a row for each data row. If the raw contact has no data rows, the cursor will still contain one row with the raw contact-level information.You will have to filter the result based on the mimeType
For example, if the mimeType is
Phone.CONTENT_ITEM_TYPE, then the columnDATA1stores the phone number, but if the data kind isEmail.CONTENT_ITEM_TYPE, thenDATA1stores the email address.This way you won’t have to use
HAS_PHONE_NUMBERas you will directly iterate trough the items.