1

I have to select from the ContactsContract table of an android device some contacts, checking the LOOKUP_KEY value of the contact.

I have to do a single query, to which I pass many LOOKUP values. So far i did something like the code you see beloew, that of course is not correct:

Cursor cursor = contentResolver.query(
                                      ContactsContract.Contacts.CONTENT_URI, 
                                      null, 
                                      ContactsContract.Contacts.LOOKUP_KEY + " =?", 
                                      new String[] {"f2hsk", "djkf7fk", "hf74fnk2"}, 
                                      null);

I tried also using ContactsContract.Contacts.LOOKUP_KEY + " IN?" but I get a synthax error.

I hope is clear what I have to achieve.

Thank you in advance

MDP
  • 4,177
  • 21
  • 63
  • 119

1 Answers1

2

Each questionmark is going to be replaced with the corresponding parameter value from the selectionArgs array.

Cursor cursor = contentResolver.query(
                                      ContactsContract.Contacts.CONTENT_URI, 
                                      null, 
                                      ContactsContract.Contacts.LOOKUP_KEY + "=? or " + ContactsContract.Contacts.LOOKUP_KEY + "=? or " + ContactsContract.Contacts.LOOKUP_KEY + "=?", 
                                      new String[] {"f2hsk", "djkf7fk", "hf74fnk2"}, 
                                      null);
artkoenig
  • 7,117
  • 2
  • 40
  • 61
  • Thank you for you help. So, if I have 100 selectArg should I write 100 times ContactsContract.Contacts.LOOKUP_KEY + "=? or "... It's seems not "clean" – MDP Jun 12 '17 at 06:50
  • No, it was just an example of how to use `selectionArgs` in the query. For the `IN` condition see [this answer](https://stackoverflow.com/a/23795007/321106) – artkoenig Jun 12 '17 at 08:00