I feel as though I may be missing something in the ContactsContract API. In my application I have several SQLite tables with references to Contacts or Groups (from the ContactsContract API). I did this rather than reinvent the wheel with my own contacts and groups tables.
Making the queries work together is turning out to be a nightmare, however. Suppose I want to perform an operation on my SQL table using all of the contacts from a particular group. I’m having to query ContactsContract to get the contact_id’s of members of a group, join those contact_id’s into a string, and then put that string into a separate query. (Or have SQL queries in a loop.)
This is such bad SQL that I feel I must be doing something wrong, but I can’t find references to any other way to do it. I’m on the verge of just maintaining my own list of contacts. Any ideas?
Nope, you aren’t on the verge of anything wrong. The Contacts ContentProvider (and pretty much every ContentProvider I’ve seen) contradicts conventional SQL wisdom, oh well.
If you want to get data from multiple types of ContactsContracts.CommonDataKinds, you need to detect the mime-type of the row you’re in and determine what kind of entity it is (or make an extra query, my preferred solution is to do it all in one query…)
Hold your breath, here’s an example of some code I wrote to do such a thing. You should be able to see how the different rows can be different kinds of entities and as a result of that their generic columns can hold different kinds of data, which is why we use CommonDataKinds.* classes to reference them contextually:
Yes this completely serious solution does use string comparisons. If there is a better way, please let me know!