views:

55

answers:

1

Hi,

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).

A: 
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();
        }
Pentium10
Thanks a lot. This worked for me like a charm
frieza
Important on SO, you have to mark accepted answers by using the tick on the left of the posted answer, below the voting. This will increase your rate.
Pentium10