Java – content resolver and “in” statement in “where” statement
•
Java
I'm trying to get the cursor of the contact list based on therir ID I'm not sure how to use an "in" statement with an array of parameters I have something wrong right now
public Cursor GetContacts(String[] ids) { ContentResolver cr = getContentResolver(); try { Uri uri = ContactsContract.Contacts.CONTENT_URI; String[] projection = new String[] { ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts.HAS_PHONE_NUMBER }; String where = ContactsContract.Contacts._ID + " IN ?"; String[] selectionArgs = ids; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME; return cr.query(uri,projection,where,selectionArgs,sortOrder); } catch (Exception ex) { String message = ex.getMessage(); Log.e("mine","Error: " + message,ex); return null; }
Error: approaching '?': Syntax error (code 1):, compile time: Select_ id,display_ name,has_ phone_ number FROM view_ contacts WHERE((1))AND((_id IN?)) ORDER BY display_ name
Solution
I didn't test this, but I think you need to compress your ID into a comma separated string to insert it into your query
public Cursor GetContacts(String[] ids) { ContentResolver cr = getContentResolver(); try { Uri uri = ContactsContract.Contacts.CONTENT_URI; String[] projection = new String[] { ContactsContract.Contacts._ID,ContactsContract.Contacts.HAS_PHONE_NUMBER }; String where = ContactsContract.Contacts._ID + " IN (?)"; String[] selectionArgs = {StringUtils.join(ids,",")}; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME; return cr.query(uri,ex); return null; }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码