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

  • Home
  • SEARCH
  • 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 6643941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:06:33+00:00 2026-05-26T00:06:33+00:00

See code to get all contacts on phone: public ContactInfo[] getContactList(String selection, String[] selectionargs)

  • 0

See code to get all contacts on phone:

public ContactInfo[] getContactList(String selection, String[] selectionargs) {

    String[] projection = new String[] {
            Contacts._ID,
            Contacts.DISPLAY_NAME,
    };

    String sortOrder = Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, 
            projection, selection, selectionargs, sortOrder);

    ContactInfo[] contactInfoList = new ContactInfo[cursor.getCount()];
    ContactInfo contactInfo;
    int i=0;

    while(cursor.moveToNext()) {

        contactInfo = new ContactInfo(cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME)), "");

        String[] projectionPhone = new String[] {
                Phone.CONTACT_ID,
                Phone.DISPLAY_NAME,
                Phone.NUMBER,
                Phone.TYPE,
        };

        Cursor phoneCursor = context.getContentResolver().query(Phone.CONTENT_URI, 
                projectionPhone, Phone.DISPLAY_NAME + "='" + contactInfo.getName() + "'", null, null);  

        PhoneInfo[] phoneInfoList = new PhoneInfo[phoneCursor.getCount()];

        int j = 0;

        while(phoneCursor.moveToNext()) {

            int type = ContactInfo.TYPE_OTHER;          

            if (phoneCursor.getInt(phoneCursor.getColumnIndex(Phone.TYPE)) == Phone.TYPE_HOME)

                type = ContactInfo.TYPE_HOME;

            else if (phoneCursor.getInt(phoneCursor.getColumnIndex(Phone.TYPE)) == Phone.TYPE_MOBILE
                    || phoneCursor.getInt(phoneCursor.getColumnIndex(Phone.TYPE)) == Phone.TYPE_WORK_MOBILE)

                type = ContactInfo.TYPE_MOBILE;

            else if (phoneCursor.getInt(phoneCursor.getColumnIndex(Phone.TYPE)) == Phone.TYPE_FAX_HOME
                    || phoneCursor.getInt(phoneCursor.getColumnIndex(Phone.TYPE)) == Phone.TYPE_FAX_WORK)

                type = ContactInfo.TYPE_FAX;

            else if (phoneCursor.getInt(phoneCursor.getColumnIndex(Phone.TYPE)) == Phone.TYPE_WORK)

                type = ContactInfo.TYPE_OFFICE;

            phoneInfoList[j++] = contactInfo.new PhoneInfo(
                    FWUtil.SeparatesCharacters(phoneCursor.getString(phoneCursor.getColumnIndex(Phone.NUMBER))),
                    type);

        }

        phoneCursor.close();


        String[] projectionEMail = new String[] {
                Email.CONTACT_ID,
                Email.DATA,
                Email.TYPE
        };

        Cursor emailCursor = context.getContentResolver().query(Email.CONTENT_URI, 
                projectionEMail, Email.CONTACT_ID + "='" + contactInfo.getName() + "'", null, null);    

        EmailInfo[] emailInfoList = new EmailInfo[emailCursor.getCount()];

        j = 0;

        while(emailCursor.moveToNext()) {

            int type = ContactInfo.TYPE_OTHER;          

            if (emailCursor.getInt(emailCursor.getColumnIndex(Email.TYPE)) == Email.TYPE_HOME)

                type = ContactInfo.TYPE_HOME;

            else if (emailCursor.getInt(emailCursor.getColumnIndex(Email.TYPE)) == Email.TYPE_MOBILE)

                type = ContactInfo.TYPE_MOBILE;

            else if (emailCursor.getInt(emailCursor.getColumnIndex(Email.TYPE)) == Email.TYPE_WORK)

                type = ContactInfo.TYPE_OFFICE;

            emailInfoList[j++] = contactInfo.new EmailInfo(
                    emailCursor.getString(emailCursor.getColumnIndex(Email.DATA)),
                    type);

        }

        emailCursor.close();

        contactInfo = new ContactInfo(cursor.getString(cursor.getColumnIndex(Contacts._ID)),cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME)),
                phoneInfoList, emailInfoList, new AddressInfo[0], "");

        contactInfoList[i++] = contactInfo;
    }

    cursor.close();

    return contactInfoList;

    //return new ContactInfo[0];

}`

This code get all contacts, but when contact has ‘ on name, like: ‘Jonh. Systems show an exception, I don’t know how to replace this charactere, because Android set query and projection before get the data no auto replace ‘ by “!!!!

  09-30 08:44:19.732: ERROR/DatabaseUtils(9292): android.database.sqlite.SQLiteException: near "Jonh": syntax error: , while compiling: SELECT contact_id, display_name, data1, data2 FROM view_data_restricted data WHERE (1 AND mimetype = 'vnd.android.cursor.item/phone_v2') AND (display_name=''Jonh')
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:330)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:7924)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:7909)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:150)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:111)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at android.os.Binder.execTransact(Binder.java:288)
09-30 08:44:19.732: ERROR/DatabaseUtils(9292):     at dalvik.system.NativeStart.run(Native Method)

Thanks for help!

  • 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-05-26T00:06:34+00:00Added an answer on May 26, 2026 at 12:06 am

    Use ‘?’ in your name selection.

    context.getContentResolver().query(URI, projection, Email.CONTACT_ID + “=?”, new String[] { contactInfo.getName()}, null);

    Have a try

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

Sidebar

Related Questions

Can't seem to figure this out, see code below. Trying to make a GET
I can't get the code below to compile (see errors). Advice on correction would
See code just bellow Our generic interface public interface Repository<INSTANCE_CLASS, INSTANCE_ID_CLASS> { void add(INSTANCE_CLASS
I have see code like this Dim s as something = new something Dim
The following code creates me an array of all my contacts in my address
See code: var file1 = 50.xsl; var file2 = 30.doc; getFileExtension(file1); //returns xsl getFileExtension(file2);
See code below, for some reason it only works when I put a breakpoint
Please see code below. The destructors are never called. Anyone know why and how
I often see code like: Iterator i = list.iterator(); while(i.hasNext()) { ... } but
Sometimes I see code like let (alt : recognizer -> recognizer -> recognizer) =

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.