0

I am making a simple music player.

This method returns the titles of all the songs on my device in a string array.

private String[] getMusic() {

        String[] projection = {
                MediaStore.Audio.Media.TITLE,
        };

        final Cursor mCursor = getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                projection, null, null,
                "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");

        int count = mCursor.getCount();

        String[] songs = new String[count];
        int i = 0;
        if (mCursor.moveToFirst()) {
            do {
                songs[i] = mCursor.getString(0);
                i++;
            } while (mCursor.moveToNext());
        }

        mCursor.close();

        return songs;
    }

The string array is passed to an adapater which populates a list view.

mMusicList = getMusic();
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);

How can I include artist, duration, and data using list adapter?

For example, my getMusic() method could begin something like this:

private String[] getMusic() {

        String[] projection = {
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.DURATION
        };

        ...

    }

I cannot figure out the rest. Maybe songs array can be made multidimensional before being passed to the adapter e.g.

songs = {

    {title1, data1, duration1},
    {title2, data2, duration2},
    etc...
}
the_prole
  • 8,275
  • 16
  • 78
  • 163

1 Answers1

0

You should create a custom adapter and pass to it yours data which will be shown in custom layout, which you should also create. There are a lot of examples in web.

Community
  • 1
  • 1
mohax
  • 4,435
  • 2
  • 38
  • 85