将歌曲从 Arraylist 传递到另一个 activity
Pass songs from Arraylist to another activity
我想创建一个非常简单的应用程序来显示歌曲列表,当点击歌曲时它会打开一个新的 Activity 来显示歌曲的播放状态,我几乎创建了它但是当我点击播放按钮它不播放。
第一个activity:
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);
}
});
正在播放 activity:
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();
}
});
}
努力改变
anotherActivityIntent.putExtra("songs",songslist);
到
anotherActivityIntent.putParcelableArrayListExtra("songs",songslist);
同时更改您使用错误的键名songs
& Songs
使双方相同
ArrayList<Songs> songs = getIntent().getParcelableArrayListExtra("songs");
您还需要在点击前投射 mMediaPlayer 对象
mMediaPlayer = (MediaPlayer)findViewById(You media player id);
未测试但是..
第一个activity:
改变
anotherActivityIntent.putExtra("songs",songslist);
到
anotherActivityIntent.putExtra("songs",song);
正在播放 activity:
改变
ArrayList<Songs> songs = getIntent().getParcelableArrayListExtra("Songs");
到
Songs songs = getIntent().getParcelableExtra("songs");
并且您需要在播放 activity 到
中添加一些代码
- 将歌曲翻译成它的路径(最好在歌曲 class 中找到路径)
- 设置 mMediaPlayer 的路径(请参阅 MediaPlayer.setDataSource(string))
- 调用 mMediaPlayer.prepare()
- 然后调用 mMediaPlayer.start()
注意:在 onClickListener 中调用 mMediaPlayer.start()
。onClick()
在您的代码中设置为 buttonPlayStop 似乎是一个拼写错误。
更多信息请参考MediaPlayer.StateDiagram or MediaPlayer developer guide。
我想创建一个非常简单的应用程序来显示歌曲列表,当点击歌曲时它会打开一个新的 Activity 来显示歌曲的播放状态,我几乎创建了它但是当我点击播放按钮它不播放。
第一个activity:
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);
}
});
正在播放 activity:
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();
}
});
}
努力改变
anotherActivityIntent.putExtra("songs",songslist);
到
anotherActivityIntent.putParcelableArrayListExtra("songs",songslist);
同时更改您使用错误的键名songs
& Songs
使双方相同
ArrayList<Songs> songs = getIntent().getParcelableArrayListExtra("songs");
您还需要在点击前投射 mMediaPlayer 对象
mMediaPlayer = (MediaPlayer)findViewById(You media player id);
未测试但是..
第一个activity:
改变
anotherActivityIntent.putExtra("songs",songslist);
到
anotherActivityIntent.putExtra("songs",song);
正在播放 activity:
改变
ArrayList<Songs> songs = getIntent().getParcelableArrayListExtra("Songs");
到
Songs songs = getIntent().getParcelableExtra("songs");
并且您需要在播放 activity 到
中添加一些代码
- 将歌曲翻译成它的路径(最好在歌曲 class 中找到路径)
- 设置 mMediaPlayer 的路径(请参阅 MediaPlayer.setDataSource(string))
- 调用 mMediaPlayer.prepare()
- 然后调用 mMediaPlayer.start()
注意:在 onClickListener 中调用 mMediaPlayer.start()
。onClick()
在您的代码中设置为 buttonPlayStop 似乎是一个拼写错误。
更多信息请参考MediaPlayer.StateDiagram or MediaPlayer developer guide。