Question

Apologies if I am missing something very simple here, I am very new to programming.

I am trying to build a media player, and have discovered and example of the use of MediaStore.Audio to collect media for a playlist. I now need to implement an adapter to get this into a listview for the user to select. I am under the impression this is probably the most elegant way of implementing a simple track list.

So far I have:

SimpleCursorAdapter myAdapter;
        String[] STAR = { "*" };
        Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

        Cursor cursor = getContentResolver().query(allsongsuri, STAR, selection, null, null);

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    String song_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    int song_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID));

                    String fullpath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));

                    String album_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                    int album_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

                    String artist_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                    int artist_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
                } while (cursor.moveToNext());
            }
            cursor.close();
        }
    }

Now I am looking to use a simpleCursorAdaptor to listView, but not sure how to go about it, or where to put it. Any help will be greatly appreciated.

Was it helpful?

Solution

You will have to implement your own SimpleCursorAdapter like you do it with Activities.

MediaStoreCursorAdapter extends SimpleCursorAdapter { . . .

To assign it to your ListView get a reference to it in OnCreate of Activity via

ListView myListView = (ListView) findViewById(R.id.mylistview);

And then assign the Adapter to it:

myListView.setAdapter(myAdapter);

If youre a beginner you might want to look into some basic tutorials to get to know language and framework a little better before diving deeper.

Good Luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top