I am trying to get the mp3 files from the sd card and put them on a listview why does this code not work it messes up when adding elements to the song name
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Artists.ARTIST };
Cursor tempCursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj,
null,
null,
null);
tempCursor.moveToFirst(); //reset the cursor
int col_index=-1;
int numSongs=tempCursor.getCount();
int currentNum=0;
do{
col_index = tempCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST);
List<String> songname = new ArrayList<String>();
if(tempCursor.moveToNext()){
songname.add(tempCursor.getString(currentNum+1));
ArrayAdapter<String> songss = new ArrayAdapter<String>(this, R.id.songs,songname);
setListAdapter(songss);
} else{
return;
}
currentNum++;
}while (tempCursor.moveToNext());
this line should be outside of the do…while() loop
as the loop iterating every time songname will define in memory with new object and you got only last name. simillary this code also after the while loop
here is the complete code