Gibran Gul
Gibran Gul

Reputation: 121

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?

Upvotes: 2

Views: 1922

Answers (2)

Armaan Stranger
Armaan Stranger

Reputation: 3130

Try this line Code:

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

Hope it Helps!!

Upvotes: 5

Alexander Mikhaylov
Alexander Mikhaylov

Reputation: 1800

The best way to use listview with cursor adapter is to use Loaders

It allow you to manage cursor by fragment and making query in background

Upvotes: 1

Related Questions