我正在使用一个mediaPlayer应用程序,我正在为我的播放列表使用ListViews。我有主要活动有mediaPlayer,当用户点击播放列表按钮时,ArtistActivity就会启动,用户可以从中启动SongActivity。我看了很多教程和例子,但由于某些原因,SongActivity正在传递" 0"返回主活动并使用歌曲URL播放ArrayList中的第一首曲目。任何帮助将非常感激。请帮忙。谢谢。
的 MainActivity: 的
btnPlaylist.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(),ArtistActivity.class);
startActivityForResult(i, 100);
}
});
}
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 50)
{
currentSongIndex = data.getExtras().getInt("songIndexArtist");
songIndex = currentSongIndex;
// play selected song
playSong(currentSongIndex);
// Displaying Song title
String songTitle =
songsList.get(currentSongIndex).get("songTitle");
songTitleLabel.setText(songTitle);
}
的 ArtistActivity: 的
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//int artistIndex = position;
// Starting new intent
Intent in2 = new Intent(getApplicationContext(),SongActivity.class);
startActivityForResult(in2,100);
setResult(100,in2);
in2.putExtra("songIndexArtist", songIndexArtist);
finish();
}
});
}
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 50)
{
songIndexArtist = data.getExtras().getInt("songIndex");
}
}
的 SongActivity: 的
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "DOWNLOADING, PLEASE WAIT", Toast.LENGTH_LONG).show();
// getting listitem index
int songIndex = position;
// Starting new intent
Intent in3 = new Intent(getApplicationContext(),
AndroidBuildingMusicPlayerActivity.class);
// Sending songIndex to PlayerActivity
in3.putExtra("songIndex", songIndex);
setResult(50,in3);
// Closing PlayListView
finish();
}
});
}
答案 0 :(得分:2)
您已混合requestCode
和resultCode
。 resultCode
应该是预定义常量之一RESULT_CANCELED
,RESULT_OK
,RESULT_FIRST_USER
等。requestCode
是您的唯一值 - 50,100等。
此requestCode
调用和同一活动的startActivityForResult
处理程序中的onActivityResult
也应相同。
例如,在 SongActivity 中,您应该致电:
setResult(RESULT_OK, in3);
并在 ArtistActivity 中,您应该致电:
startActivityForResult(in2, 50);
因为 ArtistActivity 中的onActivityResult
等待代码50。
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent in2 = new Intent(getApplicationContext(), SongActivity.class);
// Starting SongActivity and wait for results with requestCode 50
startActivityForResult(in2, 50);
}
});
}
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 50 && resultCode == RESULT_OK)
{
// get results from SongActivity (requestCode = 50)
songIndexArtist = data.getExtras().getInt("songIndex");
// now pass the same data into MainActivity
Intent result = new Intent();
result.putExtra("songIndexArtist", songIndexArtist);
setResult(RESULT_OK, result);
finish();
}
}
请注意,我将resultCode替换为requestCode。
您应该以类似的方式修复 MainActivity 。
您也可以使用FLAG_ACTIVITY_FORWARD_RESULT
flag;有一个相关的SO question和一个good explanation of this flag(以及其他标志)。如果您通过中间活动将结果传回而不进行任何处理,那么该标志是有用的,“按原样”。