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