0

我正在开发一个 mediaPlayer 应用程序,我将 ListViews 用于我的播放列表。我有具有 mediaPlayer 的主要活动,当用户单击播放列表按钮时,将启动 ArtistActivity,用户可以从中启动 SongActivity。我查看了许多教程和示例,但出于某种原因,SongActivity 将“0”传递回主要活动并使用歌曲 URL 播放 ArrayList 中的第一首曲目。任何帮助将非常感激。请帮忙。谢谢你。

主要活动:

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

    }

艺术家活动:

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");

    }

}

歌曲活动:

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();
        }
    });
}
4

1 回答 1

2

你混合了requestCoderesultCoderesultCode应该是预定义的常量之一RESULT_CANCELEDRESULT_OK等等RESULT_FIRST_USERrequestCode是您的唯一值 - 50、100 等。

此外,在同一活动中的调用和处理程序中requestCode应该是相同的。startActivityForResultonActivityResult

例如,在SongActivity你应该调用:

setResult(RESULT_OK, in3);

ArtistActivity你应该调用:

startActivityForResult(in2, 50);

因为您onActivityResultArtistActivity中等待代码 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_RESULTflag ; 有一个相关的SO question和一个很好的解释这个标志(在其他标志中)。如果您“按原样”将结果通过中间活动传回而不进行任何处理,则该标志很有用。

于 2012-12-26T10:57:14.500 回答