我想通过覆盖当前使用的ListView
来显示所选专辑的歌曲。
但我不明白。在这段代码中,我会显示所有专辑并获取他们的歌曲。
也许你可以帮助我。
非常感谢,Vinzenz:)
public class Albumsshow extends ListActivity {
public ArrayList<HashMap<String, String>> albumsList = new ArrayList<HashMap<String, String>>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse);
ArrayList<HashMap<String, String>> albumsListData = new ArrayList<HashMap<String, String>>();
AlbumsManager plm = new AlbumsManager();
// get all songs from sdcard
this.albumsList = plm.getAlbumList(this);
// looping through playlist
for (int i = 0; i < albumsList.size(); i++) {
// creating new HashMap
HashMap<String, String> album = albumsList.get(i);
// adding HashList to ArrayList
albumsListData.add(album);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, albumsListData,
R.layout.playlist_item, new String[] { "album" }, new int[] {
R.id.songTitle });
setListAdapter(adapter);
// selecting single ListView item
final ListView lv = getListView();
final TextView tv =(TextView)findViewById(R.id.umandern);
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listitem index
int albumIndex = position;
String[] column = { MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.MIME_TYPE, };
String where = android.provider.MediaStore.Audio.Media.ALBUM + "=?";
String whereVal[] = {albumsList.get(albumIndex).get("album") };
String orderBy = android.provider.MediaStore.Audio.Media.TITLE;
Cursor cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
column, where, whereVal, orderBy);
if (cursor.moveToFirst()) {
do {
Log.v("music title",
cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
} while (cursor.moveToNext());
}
}
});
}
}
答案 0 :(得分:0)
你使用的代码就是我拥有的代码并且它并不是太难,你可能最终遇到的唯一问题实际上是播放歌曲,但除此之外你可以列出所有专辑和这样的歌曲.... / p>
public class AlbumsList extends ListActivity{
public ArrayList<HashMap<String,String>> songsList = new ArrayList<HashMap<String, String>>();
int count;
Cursor cursor;
ListView musiclist;
int songAlbum;
int position;
ListView list;
String songTitle = "";
String songPath = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.albums);
list = (ListView) findViewById(android.R.id.list);
String[] columns = { BaseColumns._ID,
AlbumColumns.ALBUM };
cursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
columns, null, null, null);
String[] displayFields = new String[] { AlbumColumns.ALBUM };
int[] displayViews = new int[] { android.R.id.text1 };
@SuppressWarnings("deprecation")
ListAdapter adapter = (new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, displayFields,
displayViews));
setListAdapter(adapter);
}
public AlbumsList(){
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if(cursor.moveToPosition(position)){
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
// get all songs from sdcard
this.songsList = this.getPlayList();
// looping through playlist
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
ListAdapter adapter = new SimpleAdapter(this, songsListData,
android.R.layout.simple_list_item_1, new String[] { "songTitle", "songPath" }, new int[] {
android.R.id.text1});
setListAdapter(adapter);
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listitem index
int songIndex = position;
// Starting new intent
Intent in = new Intent(AlbumsList.this,
MainActivity.class);
Log.d("TAG","onItemClick");
// Sending songIndex to PlayerActivity
in.putExtra("songPath", songIndex);
startActivityForResult(in, 99);
// Closing PlayListView
finish();
}
});
}
}
public ArrayList<HashMap<String, String>> getPlayList(){
String[] columns = { MediaColumns.DATA,
BaseColumns._ID,
MediaColumns.TITLE,
MediaColumns.MIME_TYPE, };
String where = AudioColumns.ALBUM
+ "=?";
String whereVal[] = { cursor.getString(cursor
.getColumnIndex(AlbumColumns.ALBUM))};
String orderBy = MediaColumns.TITLE;
cursor = this.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,columns,
where, whereVal, orderBy);
if (cursor.moveToFirst()) {
do {
songTitle = cursor.getString(cursor.getColumnIndexOrThrow(MediaColumns.TITLE));
songPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaColumns.DATA));
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", songTitle);
song.put("songPath", songPath);
songsList.add(song);
} while (cursor.moveToNext());
}
cursor.close();
return songsList;
}