Getting contacts when ids are known
Question
I have a list of 'n' contact ids corresponding to which I need to obtain the contact details. One simple way to make n queries using the contact ids and retrieve those contacts. But this will be very time-consuming especially if n is large. I would like to know if there is any simpler way to obtain these results (like batch query etc).
Solution
ContentResolver cr = context.getContentResolver();
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI, projection,
ContactsContract.Contacts._ID + " in ("+comma_delimited_ids+") , null,
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
then you can loop the cursor
if (c!=null) {
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
// your code to get details from cursor
}
c.close();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow