1

I want to put all the contacts in my phone into an array. I am using the code below but it takes 3-4 seconds to put all of them into the array. To accelerate the process, I am collecting only those contacts who has a phone number. This results in an array of 163 elements.

final String[] projection1 = new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER};
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection1, null, null, null);
        if (cur.getCount() > 0) 
        {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Cursor pCur = cr.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                while (pCur.moveToNext()) {
                    number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    contactnumbers.add(number);

                } 
                pCur.close();
                }
            }
        }      

Why is this so slow?

erdomester
  • 11,789
  • 32
  • 132
  • 234
  • From my experiences, the ContactsContract seems to run very slowly on the emulator. Try your application on a device and see if it runs any faster. – Adil B Oct 19 '11 at 04:40
  • I would not have raised this question if I had not tried it out on my phone. – erdomester Oct 19 '11 at 07:10

1 Answers1

1

You have a query inside a query. Why don't you create a joined query where you join both statements. This will be probably much faster than the current solution.

Drejc
  • 14,196
  • 16
  • 71
  • 106
  • How to create a joined query with two uris? AFAIK Phone numbers are stored in their own table and need to be queried separately. – erdomester Oct 18 '11 at 20:38
  • Take a look at this question: http://stackoverflow.com/questions/4957009/how-do-i-join-two-sqlite-tables-in-my-android-application – Drejc Oct 19 '11 at 19:18