【问题标题】:Fetch Genre name list which have songs in it获取其中包含歌曲的流派名称列表
【发布时间】:2014-08-26 04:21:01
【问题描述】:

我正在使用 CursorLoder 类从 android 的媒体内容提供商获取流派列表。 下面是我用来获取流派列表的光标查询。

 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // currently filtering.
        Uri baseUri;
        baseUri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
        String[] STAR = { "*" };
        return new CursorLoader(getActivity(), baseUri, STAR, null, null, null );
    }

现在,我从媒体内容提供商那里获得了所有类型列表,但我获得的数据存在问题。我也得到了之前创建的流派名称,但现在我没有歌曲。

我只想要里面有歌曲的类型,而不是里面没有歌曲的类型名称。

谁能帮帮我?

更新

我可以使用以下代码获取所有流派及其歌曲...

private void getGenresList() {

        //-------------------------------------

        int index;
        long genreId;
        int count;
        Uri uri;
        Cursor genrecursor;
        Cursor tempcursor;
        String[] proj1 = {MediaStore.Audio.Genres.NAME, MediaStore.Audio.Genres._ID};   
        String[] proj2={MediaStore.Audio.Media.DISPLAY_NAME};

         genrecursor=getActivity().getContentResolver().query(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI,proj1,null, null, null);
         if(genrecursor.moveToFirst())
         {
             do{
                index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);            
                System.out.println("GENRE NAME: "+genrecursor.getString(index));
                System.out.println("======================================");

                index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);             
                genreId=Long.parseLong(genrecursor.getString(index));
                uri = MediaStore.Audio.Genres.Members.getContentUri("external", genreId);

                tempcursor = getActivity().getContentResolver().query(uri, proj2, null,null,null);
                System.out.println("Total Songs: "+tempcursor.getCount());
                if(tempcursor.moveToFirst())
                {
                    do{
                        index=tempcursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                        System.out.println("    Song Name: "+tempcursor.getString(index));

                    }while(tempcursor.moveToNext());
                }
                System.out.println("======================================");
             }while(genrecursor.moveToNext());       
         }


        //-------------------------------------

    }

但是,我必须使用两个查询,一个用于获取所有流派,第二个用于过滤包含歌曲的流派的数据。即使我想在 CursorLoader 中使用它,所以我想知道如何做到这一点。

希望你对这个问题有正确的想法。

【问题讨论】:

    标签: android android-contentprovider simplecursoradapter android-cursorloader


    【解决方案1】:

    似乎没有其他方法可以实现这一点,所以我只需要像下面这样...... 通过以下更新,我可以获得其中只有歌曲的流派列表。过滤掉没有歌曲的流派列表。

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    
            Uri baseUri;
            if (mCurFilter != null) {
                baseUri = Uri.withAppendedPath(
                        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                        Uri.encode(mCurFilter));
            } else {
                baseUri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
            }
    
            String[] STAR = { MediaStore.Audio.Genres._ID,
                    MediaStore.Audio.Genres.NAME };
    
    
            String query = " _id in (select genre_id from audio_genres_map where audio_id in (select _id from audio_meta where is_music != 0))" ;
    
            return new CursorLoader(getActivity(),
                    MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, STAR, query,
                    null, null);
    
        }
    

    在上面的代码中,我加入了流派表和音频表。通过这种方式,我可以获取该流派是否有歌曲。所以现在我只有那种有歌曲的流派。

    希望这对其他人也有帮助。

    享受编码... :)

    【讨论】:

    • 天哪,我讨厌这样一个事实,即我们已经经历了过滤掉没有曲目的流派的所有麻烦。首先用孤儿类型污染 MediaStore 有什么意义?
    • 什么是 mCurFilter ?
    • @jack 请参考:developer.android.com/intl/ja/reference/android/net/…, java.lang.String)
    • mCurFilter 在哪里初始化?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2015-11-04
    • 1970-01-01
    • 2016-05-08
    • 2013-12-28
    • 1970-01-01
    • 2018-03-08
    相关资源
    最近更新 更多