2

我得到了流派:

public static CursorLoader getGenres(Context context) {
    Uri uri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
    String[] columns = { MediaStore.Audio.Genres._ID,
            MediaStore.Audio.Genres.NAME };
    String orderBy = MediaStore.Audio.Genres.NAME;

    return new CursorLoader(context, uri, columns, null, null, orderBy);
}

但我需要每种类型的歌曲数量。我可以为每种类型执行此操作:

private int getNumberSongsOfGenre(long genreID) {
    Uri uri = MediaStore.Audio.Genres.Members.getContentUri(VOLUMENAME,
            genreID);
    Cursor c = resolver.query(uri, null, null, null, null);

    if (c == null || c.getCount() == 0)
        return -1;

    int num = c.getCount();
    c.close();

    return num;
}

...但我需要在同一个查询中执行此操作并返回一个 CursorLoader。任何想法?

4

2 回答 2

0

我也在寻找同样的东西,但仍然没有得到任何合适的解决方案。但是,如果您只关心要显示的歌曲数量,那么您可以在下面做类似我的回答。

@Override
    public void bindView(View v, Context context, Cursor c) {
        int index;
        long genreId;
        Uri uri;
        ViewHolder holder  =   (ViewHolder)    v.getTag();

         holder.titleName.setText(c.getString(c.getColumnIndex(MediaStore.Audio.Genres.NAME)));
         //===========================

         index = c.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);            
         //Log.i("Tag-Genre name", c.getString(index));

         index = c.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);             
         //genreId=Long.parseLong(c.getString(index));
         genreId  = c.getLong(c.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID));
         uri = MediaStore.Audio.Genres.Members.getContentUri("external", genreId);
         String[] proj2={MediaStore.Audio.Media.DISPLAY_NAME};
         Cursor tempcursor = context.getContentResolver().query(uri, proj2, null,null,null);
         //tempcursor.moveToFirst();
         //Log.i("Tag-Number of songs for this genre", tempcursor.getCount()+"");
         holder.artistName.setText(""+tempcursor.getCount()+" Songs");

    }

如果您只关心其中显示的歌曲数量,希望您能得到答案。

于 2014-08-26T06:34:21.330 回答
0

尝试这个

ArrayList<GenreInfo> infos = new ArrayList<>();
    Cursor c = getAllGenre(search);
    if (c!=null) {
        c.moveToFirst();
        while (!c.isAfterLast()) {
            GenreInfo genreInfo = new GenreInfo();
            genreInfo.setID(c.getString(c.getColumnIndex(MediaStore.Audio.Genres._ID)));
            genreInfo.setName(c.getString(c.getColumnIndex(MediaStore.Audio.Genres.NAME)));
            Cursor count = ourContext.getContentResolver().query(MediaStore.Audio.Genres.Members.
                    getContentUri("external", Long.parseLong(genreInfo.getID())), null, null, null, null);
            genreInfo.setNumOfSongs(count.getCount());
            infos.add(genreInfo);
            count.close();
            c.moveToNext();
        }
    }
    if (c!=null) c.close();
    return infos;
于 2017-03-09T21:16:40.857 回答