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 8066661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:04:44+00:00 2026-06-05T12:04:44+00:00

I am trying to get contacts of the android device. I found a great

  • 0

I am trying to get contacts of the android device.
I found a great sample code here

However, when trying to populate a ListActivity with ArrayAdapter that is using that code it is taking a lot of time – almost 4 seconds on galaxy s2 and much more on older devices.
I need to improve that performance. What I thought is to implements Cursor that will hold more than one dimension Cursor, and use SimpleCursorAdapter as the list adapter.
I see few problems with this approach:

  • I don’t know how to get item at specific position.
  • each cursor has different columns.

Is there a better/easier way to do that?

EDIT:

here is my code:

public List<ContactData> readContacts(){
    ContentResolver cr = getContentResolver();
    List<ContactData> cd = new ArrayList<ContactData>();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
           null, null, null, ContactsContract.Contacts.DISPLAY_NAME);

    if (cur.getCount() > 0) {
       while (cur.moveToNext()) {
           String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
           String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
           if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

               // get the phone number
               Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                      ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                      new String[]{id}, null);
               while (pCur.moveToNext()) {
                     String phone = pCur.getString(
                            pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                     if (!Utils.isEmpty(phone)) {
                         cd.add(new ContactData(id, name, phone));
                     }
               }
               pCur.close();


           }
       }
  }
    return cd;
}

EDIT 2:

managed to fix it by changing it to only one query:

public List<ContactData> readContacts(){
    ContentResolver cr = getContentResolver();
    List<ContactData> cd = new ArrayList<ContactData>();
               Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                       new String[]{ContactsContract.CommonDataKinds.Phone._ID,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY},
                       ContactsContract.CommonDataKinds.Phone.NUMBER + " IS NOT NULL",
                                      null, 
                                      ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY);
               while (pCur.moveToNext()) {
                     String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                     String id = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
                     String name = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY));
                         ContactData cd1 = contactDataProvider.get();
                         cd1.id = id;
                         cd1.name = name;
                         cd1.phone = phone;
                         cd.add(cd1);
               }
               pCur.close();
    return cd;
}
  • 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-05T12:04:46+00:00Added an answer on June 5, 2026 at 12:04 pm

    You can get name and ID at the same time you get the phone number:

    String[] PROJECTION=new String[] {Contacts._ID, Contacts.DISPLAY_NAME, Phone.NUMBER};
    Cursor pCur = cr.query(Phone.CONTENT_URI, PROJECTION, Phone.CONTACT_ID +" = ?",
                                          new String[]{id}, null);
    

    This eliminates the per-row sub-query.

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

Sidebar

Related Questions

I am trying to read Android device contacts in VCard format using Android Api.
I am trying to get all contacts in the favourites list of the Android
I'm trying to simply get contacts from the Device phone book and display them
I'm trying to get my contacts on a list view. Now I know that
I'm trying to get the contacts from Outlook (2007) using the following code: Outlook.Application
I am trying to get all contacts that have a phone number, and record
I was trying to get the contacts using a query through the ContactsContract The
I am trying to get access to all the endpoints that a particular contact
Im trying to get an object form my SQLite database in my Android application.
I'm using Trigger.io for an iOS / Android app and trying to get a

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.