2

如何在片段内使用光标?这是我要设置的游标片段,但它说 managedQuery 未定义:

片段1.java

private List<String> getMp3FilesFromSDCard() {
    //here we get the list of songs stored on the SD card and we update our singleton class     
        List<String> result = new ArrayList<String>();

        SingletonPlaylist playlist;

        playlist = SingletonPlaylist.getInstance();

        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor cursor;
        String[] projection = 
        {
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DISPLAY_NAME
        };

        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";


        cursor = this.managedQuery(uri, projection, selection, null, null);

        cursor.moveToFirst();

        while (cursor.moveToNext())
        {
            result.add(cursor.getString(5));
            playlist.addSong(new Song(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5)));
        }


        return result;
    }

我怎样才能解决这个问题?

4

2 回答 2

5

试试这行代码:

getActivity().getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder)

希望能帮助到你!!

于 2013-08-12T12:32:09.453 回答
1

将 listview 与光标适配器一起使用的最佳方法是使用Loaders

它允许您按片段管理光标并在后台进行查询

于 2013-08-12T12:41:00.830 回答