Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8090845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:51:31+00:00 2026-06-05T19:51:31+00:00

I feel as though I may be missing something in the ContactsContract API. In

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:51:34+00:00Added an answer on June 5, 2026 at 7:51 pm

    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:

    public class ContactsHelper {
    
    
        private static String[] PROJECTION = {
            Data.CONTACT_ID,
            Data.MIMETYPE,
            StructuredName.GIVEN_NAME,
            StructuredName.FAMILY_NAME,
            StructuredName.DISPLAY_NAME,
            StructuredName.MIDDLE_NAME,
            Email.ADDRESS
        };
    
        public static void readContacts(Context context) {
            ContentResolver resolver = context.getContentResolver();
            Uri contactsUri = ContactsContract.Data.CONTENT_URI;
    
            SQLiteDatabase conn = DatabaseHelper.openDatabase(context);
    
            Cursor cursor = resolver.query(contactsUri, PROJECTION, null, null, null);
    
            int idxContactId = cursor.getColumnIndex(Data.CONTACT_ID);
            int idxMimeType = cursor.getColumnIndex(Data.MIMETYPE);
            int idxGivenName = cursor.getColumnIndex(StructuredName.GIVEN_NAME);
            int idxFamilyName = cursor.getColumnIndex(StructuredName.FAMILY_NAME);
            int idxDisplayName = cursor.getColumnIndex(StructuredName.DISPLAY_NAME);
            int idxMiddleName = cursor.getColumnIndex(StructuredName.MIDDLE_NAME);
            int idxEmail = cursor.getColumnIndex(Email.ADDRESS);
    
            for (cursor.moveToFirst(); ! cursor.isAfterLast(); cursor.moveToNext()) {
                String mimeType = cursor.getString(idxMimeType);
    
                Integer contactId = cursor.getInt(idxContactId);
    
                if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
                    String firstName = cursor.getString(idxGivenName);
                    String middleName = cursor.getString(idxMiddleName);
                    String lastName = cursor.getString(idxFamilyName);
                    String displayName = cursor.getString(idxDisplayName);
    
                                Log.d(TAG, all the values ^^^);
    
                }
    
    
                if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
                    String emailName = cursor.getString(idxEmail);
                    Log.d(TAG, all the values ^^^);
                }
            }
    
            DatabaseHelper.closeDatabase(conn);
            cursor.close();
        }   
    }
    

    Yes this completely serious solution does use string comparisons. If there is a better way, please let me know!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel as though this this is a simple question, but can't find an
I feel as though this will end up being a silly question with a
Having read and googled to the point of exhaustion, I feel as though I
I know this may not be exactly a coder question, but I feel it
I am building a MVC 3 application where the users may not be in
I think have a good feel to this based on my research but would
I have a checkboxlist in ASP.net with several choices in it. Is there a
The difficult here may be the result of bad table design, feel free to
I may be going about this backwards... I have a class which is like
I feel as though there is a really simple solution to my problem, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.