Cursor for listview in a fragment

How can i use a cursor inside a fragment? Here is the snippet of a cursor i want to setup but it says managedQuery is undefined:

Fragment1.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;
    }

How can i fix this?

Answer

Try this line Code:

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

Hope it Helps!!

Leave a Reply

Your email address will not be published. Required fields are marked *