另一个子活动中的子活动



我正在制作一个mediaPlayer应用程序,我正在使用ListViews作为我的播放列表。我有主活动,其中有mediaPlayer,当用户点击播放列表按钮,ArtistActivity启动,用户可以启动SongActivity。我看过很多教程和例子,但由于某种原因,SongActivity将"0"传递回主活动,并在歌曲url的数组列表中播放第一个轨道。任何帮助都将非常感激。请帮助。谢谢你!

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

您已经混合了requestCoderesultCoderesultCode为预定义常数RESULT_CANCELEDRESULT_OKRESULT_FIRST_USER等中的一个。requestCode是您的唯一值- 50,100等

同样,requestCodestartActivityForResult调用和onActivityResult处理程序中应该是相同的。

例如,在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_RESULT标志;有一个相关的SO问题和一个很好的解释这个标志(在其他标志中)。如果您将结果"按原样"传递回中间活动而不进行任何处理,则该标志很有用。

相关内容

  • 没有找到相关文章

最新更新