Try this for songs.
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
public ArrayList<HashMap<String, String>> getPlayList(Context c) {
final Cursor mCursor = c.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaColumns.TITLE, MediaColumns.DATA, AudioColumns.ALBUM }, null, null,
"LOWER(" + MediaColumns.TITLE + ") ASC");
String songTitle = "";
String songPath = "";
if (mCursor.moveToFirst()) {
do {
songTitle = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaColumns.TITLE));
songPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaColumns.DATA));
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", songTitle);
song.put("songPath", songPath);
songsList.add(song);
} while (mCursor.moveToNext());
}
mCursor.close();
return songsList;
}
and then this is to add songs to the list
public class PlayListActivity extends ListActivity {
public ArrayList<HashMap<String,String>> songsList = new ArrayList<HashMap<String, String>>();
ListView musiclist;
Cursor mCursor;
int songTitle;
int count;
int songPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
ArrayList<HashMap<String, String>> tempSong = new ArrayList<HashMap<String, String>>();
SongsManager plm = new SongsManager();
this.songsList = plm.getPlayList(this);
for (int i = 0; i < songsList.size(); i++) {
HashMap<String, String> song = songsList.get(i);
tempSong.add(song);
}
ListAdapter adapter = new SimpleAdapter(this, tempSong,
android.R.layout.simple_list_item_1, new String[] { "songTitle", "songPath" }, new int[] {
android.R.id.text1});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int songIndex = position;
Intent in = new Intent(getApplicationContext(),
MainActivity.class);
Log.d("TAG","onItemClick");
in.putExtra("songPath", songIndex);
setResult(100, in);
finish();
}
});
}
Maybe someone has a better way to do this, but I know 100% that it works!
source
share