How load all the contacts with minimum time in Android

Bharathi D picture Bharathi D · Jun 1, 2012 · Viewed 16.4k times · Source

In my project getting contacts is taking a long time to load.

  • What are ways to reduce the time of getting contacts
  • Assume there are 1000 contacts in my phone.
  • Right now it is taking more than 2 minutes to load all the contacts

How can I reduce the time to load contacts ? Any Thoughts?

I referred to the the following link when programming the initial method.

http://www.coderzheaven.com/2011/06/13/get-all-details-from-contacts-in-android/

Answer

Melbourne Lopes picture Melbourne Lopes · Feb 24, 2015

BETTER SOLUTION HERE.....

private static final String[] PROJECTION = new String[] {
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
        ContactsContract.Contacts.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.NUMBER
    };
.
.
.

ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, null);
        if (cursor != null) {
            try {
                final int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                final int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

                String name, number;
                while (cursor.moveToNext()) {
                    name = cursor.getString(nameIndex);
                    number = cursor.getString(numberIndex);
                }
            } finally {
                cursor.close();
            }
        }

CHEERS...:)