In my Android application, an Activity is to display the list of contacts in the user's phone. The issue is that the operation takes about 3 seconds to execute, needless to say this is completely unacceptable. Here is the code I used:
private List<Contact> retrieveContacts(ContentResolver contentResolver) {
LinkedHashSet<Contact> contactSet = new LinkedHashSet<Contact>();
List<Contact> listContacts = new ArrayList<Contact>();
final Cursor cursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER }, null,
null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor == null);
if (cursor.moveToFirst() == true) {
do {
final String name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
final String telephone = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contact contact = new Contact(telephone, name, false, false);
contactSet.add(contact);
} while (cursor.moveToNext() == true);
}
if (cursor.isClosed() == false) {
cursor.close();
}
for (Contact contact : contactSet){
listContacts.add(contact);
}
return listContacts;
}
I then populate a ListView with the result of retrieveContacts. I'm using LinkedHashSet because it doesn't accept duplication and it keeps the order used to add elements. (I needed to retain that order)
Is this standard for such a query to take that much time? Is there any way to improve its performance?