将歌曲从Arraylist传递到另一个活动

时间:2017-12-24 15:35:09

标签: android arraylist android-mediaplayer parcelable

我想创建一个非常简单的应用程序,显示一个歌曲列表,当点击歌曲它打开一个新的活动,以显示该歌曲的播放雕像,我几乎创建了所有,但当我点击播放按钮它没有& #39; t play。

第一项活动:

    private Button buttonPlayStop;
    private MediaPlayer mMediaPlayer;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);

    final ArrayList<Songs> songslist = new ArrayList<Songs>();
    songslist.add(new Songs("hhhhh", "cHU CHU ", R.drawable.mimi, R.raw.hhh));
    songslist.add(new Songs("hhhh", "cHU CHU ", R.drawable.jes1s, R.raw.hhh));
    songslist.add(new Songs("hhhh", "cHU CHU ", R.drawable.matt, R.raw.hhh));
    songslist.add(new Songs("hhhhh", "cHU CHU ", R.drawable.freind, R.raw.hhh));
    songslist.add(new Songs("hhhhh", "cHU CHU ", R.drawable.joe, R.raw.hhh));
    songslist.add(new Songs("hhhhh ", "cHU CHU ", R.drawable.bada, R.raw.hhh));
    songslist.add(new Songs("hhhhh", "cHU CHU ", R.drawable.abby, R.raw.hhh));

    final SongsAdapter adapter = new SongsAdapter(this, songslist);

    final ListView listView = (ListView) findViewById(R.id.listview);

    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new   AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapterView, View  view, int position, long l) {
            Songs song = songslist.get(position);

            Intent anotherActivityIntent = new  Intent(FunActivity.this, playingActivity.class);
            anotherActivityIntent.putExtra("songs",songslist);
            startActivity(anotherActivityIntent);

        }
    });

播放活动:

    public class playingActivity extends FunActivity {
    private MediaPlayer mMediaPlayer;
    private Button buttonPlayStop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_playing);
    ArrayList<Songs> songs =                    getIntent().getParcelableArrayListExtra("Songs");
    buttonPlayStop = (Button)findViewById(R.id.ButtonPlayStop);
    buttonPlayStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           mMediaPlayer.start();
        }
    });

   }

2 个答案:

答案 0 :(得分:1)

未经测试,但..

第一项活动:

更改
anotherActivityIntent.putExtra("songs",songslist);


anotherActivityIntent.putExtra("songs",song);

播放活动:

更改
ArrayList<Songs> songs = getIntent().getParcelableArrayListExtra("Songs");

Songs songs = getIntent().getParcelableExtra("songs");

你需要在播放活动中添加一些代码   - 将歌曲翻译成它的路径(也许最好在Songs类中有路径)
  - 设置mMediaPlayer的路径(请参阅MediaPlayer.setDataSource(string)
  - 致电mMediaPlayer。prepare()
  - 然后拨打mMediaPlayer.start()

注意:在onClickListener中调用mMediaPlayer.start()onClick()在代码中设置为buttonPlayStop似乎是一个错字。

请参阅MediaPlayer.StateDiagramMediaPlayer developer guide了解更多信息。

答案 1 :(得分:0)

尝试改变

 anotherActivityIntent.putExtra("songs",songslist);

 anotherActivityIntent.putParcelableArrayListExtra("songs",songslist);

同时更改错误使用的密钥名称songs&amp; Songs为双方提供相同的内容

ArrayList<Songs> songs = getIntent().getParcelableArrayListExtra("songs");                    

你还需要在onclick lik之前投射mMediaPlayer对象

mMediaPlayer = (MediaPlayer)findViewById(You media player id);