【问题标题】:How can I get the genres and the number of songs of each genres in one query in Android?如何在 Android 的一次查询中获取流派和每种流派的歌曲数量?
【发布时间】:2013-01-10 19:39:11
【问题描述】:

我知道流派:

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

【问题讨论】:

    标签: android audio cursor mediastore


    【解决方案1】:

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

    @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");
    
        }
    

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

    【讨论】:

    • 你有什么发现吗?
    【解决方案2】:

    试试这个

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多