获取给定媒体商店类型的成员数,而无需加载额外的游标

时间:2017-01-02 19:22:51

标签: java android mediastore

我在RecyclerView中显示了我的设备中存在的所有类型的列表,媒体存储查询的内容是在BingViewHolder上获取的。我还想显示每种类型的歌曲数量以及该类型的名称。我有类型ID,可以计算为

int count;
            String[] projection = {MediaStore.Audio.Genres.Members.AUDIO_ID};
            String selection = MediaStore.Audio.Genres.Members.IS_MUSIC + "!=0";
            Uri uri = MediaStore.Audio.Genres.Members.getContentUri("external", id);
            Cursor tempcursor = fragmentActivity.getContentResolver().query(uri, projection, selection, null, null);
            if(tempcursor != null) {
                count = tempcursor.getCount();
                tempcursor.close();
            }

但是,由于我在onBindViewHolder中获取流派游标内容,因此我无法获得额外的查询,这会妨碍滚动的平滑性。有一个名为MediaStore.Audio.Genres._COUNT的字段,但它在运行时没有发现这样的列发现错误。如果有人可以指出一个保持滚动可接受的解决方案并且不会大幅改变我现有的应用程序结构,那将会非常有用。提前致谢

1 个答案:

答案 0 :(得分:1)

This is the way I do it in my app New Playlist Manager

      public int getgenre_Count(Context context, long genreID) {
    final String track_id = MediaStore.Audio.Genres.Members._ID;
    final ContentResolver resolve = context.getContentResolver();
    Uri uri = MediaStore.Audio.Genres.Members.getContentUri("external",
            genreID);
    final String[] columns = {track_id};
    Cursor cur = resolve.query(uri, columns, null, null, null);
    int genrecnt = 0;
    if (cur != null && cur.moveToFirst()) {
        genrecnt = cur.getCount();
        cur.close();
    }
    return genrecnt;
}

I do this in the adapter and it behaves fine.