Android Content Provider如何查询多个selectionArg

Android Content Provider how to query with multiple selectionArg

我有 table 如下所示:

学生:

id   |  student_name
------------------
1    |  max
2    |  alex
3    |  james

我只想查询 ID 为 13 的学生。

getContentResolver().query(StudentContentProvider.CONTENT_URI2, projection,"id=?",selectionArg,null);

我必须在 selectionArg 中写些什么,所以我只能得到 ID 为 1 和 3 的学生?

你试过了吗

getContentResolver().query(StudentContentProvider.CONTENT_URI2, projection,"id=1 or id=3",null,null);

或者如果您使用占位符“?” selectionArg 必须包含值“1”和“3”

getContentResolver().query(StudentContentProvider.CONTENT_URI2, projection,"id=? or id=?",selectionArg,null);

我找到的答案是"selection"加id in (?,?),给"selectionArgs[]"查询。 ? 标志替换为 "selectionArgs[]" 内的元素。你必须为每个元素添加 ? 所以我这样做了:

String selection1 = DatabaseOpenHelper.COLUMN_ID + " in (";
for (int i = 0; i < selectionArgs1.length; i++) {
    selection1 += "?, ";}           
selection1 = selection1.substring(0, selection1.length() - 2) + ")";            
getContentResolver().query(StudentContentProvider.CONTENT_URI1, projection1, selection1, selectionArgs1, null);