如何在Android中的一个查询中获取每种类型的流派和歌曲数量?

时间:2013-01-10 19:39:11

标签: android audio cursor mediastore

我得到的类型:

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。有什么想法吗?

2 个答案:

答案 0 :(得分: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");

    }

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

答案 1 :(得分: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;