erdomester
erdomester

Reputation: 11829

Android ContactsContract.Contacts slow

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?

Upvotes: 1

Views: 614

Answers (1)

Drejc
Drejc

Reputation: 14286

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.

Upvotes: 1

Related Questions