Reputation: 651
Is there a faster method for reading contacts in android? For example my method with cursor take 3-5 seconds for reading 30-50 contacts. It's very long.
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
names.add(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
numbers.add(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phones.close();
}
}
Any ideas?
Upvotes: 1
Views: 1770
Reputation: 312
Sorry for my bad english. I have created the similar but with different style using HashMap. I will paste my approach.
//Create a hashMap, Key => Raw Contact ID and value => Object of customClass
HashMap<Long, ContactStructure> mFinalHashMap = new HashMap<Long, ContactStructure>();
//Run IN query in data table. example
select mimetype_id, raw_contact_id, data1 to data14 from data where raw_contact_id IN (select _id from raw_contacts where deleted <> 1 and account_type = "phone" and account_name = "bla bla") and mimetype_id = (select _id from mimetypes where mimetype = "vnd.something.phone");
Now Create a class which will have all the data of contact.
while accessing the cursor.
while (cursor.moveToNext()) {
ContactStructure contactStructure = mFinalHashMap.get(rawContactID);
//It will return the previous instance of object, If we already put
if(rawContactStructure == null) {
contactStructure = ContactStructure.provideInstance();
}
//Now check for your required mimeType
case MIMETYPE_PHONE:
contactStructure.hasPhoneNo = true;
contactStructure.phoneNumbers.add(addDetail); //add the data1 .. to data14
break;
}
/*Demo class for saving the details*/
public class ContactMetaData {
static classContactStructure {
boolean hasPhoneNo;
List<List<String>> phoneNumbers;
public static ContactStructure provideInstance() {
contact.phoneNumbers = new ArrayList<List<String>>();
ContactStructure contact = new RawContactStructure();
return contact
}
}
use this approach, I tried with 3000 contacts with all the data, It was fast. coz its not easy to get all the contacts and their all data with in sec..
Upvotes: 2
Reputation: 1856
your question is interesting....
reading a fast contacts because it take time to read contacts data from ContactsContract..
i don't know about another way then the one you use, but still u can increase the performance by providing a String[] projection parameter to managedQuery...
fetch only those data which u require from contactsContract, because by providing null value it fetches all the columns of the record.
Upvotes: 2