如何从同一列的多个值中获取行

How to get rows from multiple values of the same column

有了这个语句,我得到一行 MediaStore.Audio.Albums._ID = 16:

    String[] stringArray = {"16"};
    final Cursor mCursor = getContentResolver().query(
            MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, // Uri
            new String[]{                                 // String[] projection (columns)
                    MediaStore.Audio.Albums.ALBUM_ART,
                    MediaStore.Audio.Albums.ARTIST,
                    MediaStore.Audio.Albums.ARTIST,
                    MediaStore.Audio.Albums.NUMBER_OF_SONGS,
                    MediaStore.Audio.Albums.ALBUM_KEY
            },
            MediaStore.Audio.Albums._ID + "=?",           // String selection
            stringArray,                                  // String[] selectionArgs
            null,                                         // sortOrder
            null                                          // CancellationSignal
    );

现在如何获取同一列具有多个值的多行,例如MediaStore.Audio.Albums._ID = 16,25,24,30,...etc?

是这样的吗?

String[] stringArray = {"16","25","24","30","14","31","28","23","17","16","13"}; 
final Cursor mCursor = getContentResolver().query(
        MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, // Uri
        new String[]{                                 // String[] projection (columns)
                MediaStore.Audio.Albums.ALBUM_ART,
                MediaStore.Audio.Albums.ARTIST,
                MediaStore.Audio.Albums.ARTIST,
                MediaStore.Audio.Albums.NUMBER_OF_SONGS,
                MediaStore.Audio.Albums.ALBUM_KEY
        },
        MediaStore.Audio.Albums._ID + "=?",           // String selection
        stringArray,                                  // String[] selectionArgs
        null,                                         // sortOrder
        null                                          // CancellationSignal
);

您的 mSelectionClause MediaStore.Audio.Albums._ID + "=?", 不正确。

Documentation 表示您需要一个“?”对于每个替换的参数。

使用运算符 IN: http://www.w3schools.com/sql/sql_in.asp

final Cursor mCursor = getContentResolver().query(
        MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, // Uri
        new String[]{                                 // String[] projection (columns)
                MediaStore.Audio.Albums.ALBUM_ART,
                MediaStore.Audio.Albums.ARTIST,
                MediaStore.Audio.Albums.ARTIST,
                MediaStore.Audio.Albums.NUMBER_OF_SONGS,
                MediaStore.Audio.Albums.ALBUM_KEY
        },
        MediaStore.Audio.Albums._ID + " IN(?,?)",           // String selection
         new String[]{"16","25"},                                  // String[] selectionArgs
        null,                                         // sortOrder
        null                                          // CancellationSignal
);

希望对您有所帮助。