【问题标题】:show songs of an selected album in Listview在列表视图中显示选定专辑的歌曲
【发布时间】:2013-12-21 11:52:55
【问题描述】:

我想通过覆盖当前使用的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());
            }
    }

    });


}
}

【问题讨论】:

    标签: android eclipse listview mediastore


    【解决方案1】:

    您使用的代码是我拥有的代码,它并不太难,您最终可能遇到的唯一问题是实际播放歌曲,但除此之外,您可以列出所有专辑和这样的歌曲....

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      相关资源
      最近更新 更多