我想知道在执行Android应用时如何跳转到另一行代码。
这是我的问题。
首先,这是我的代码: - 。
listView.setOnItemClickListener
(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
int Current_Song;
Songs song = Song.get(i);
//If mediaPlayer is not used before, this will make oldsong as present song.
if (Old_Song == -326523) {
Old_Song = song.getSong();
}
Current_Song = song.getSong();
ImageView IVP_P = (ImageView) findViewById(R.id.P_PImage);
//If mediaPlayer is paused.
if (IsPaused) {
P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);
//If the song paused is same as the new song.
if (Current_Song == Old_Song) {
mediaplayer.start();
}
//If the song Paused is not the new song.
else {
if (mediaplayer != null) {
mediaplayer.release();
mediaplayer = null;
}
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mediaplayer = mediaplayer.create(SongsListActivity.this, song.getSong());
Old_Song = song.getSong();
NameD.setText(song.getNameOfSong());
RateD.setText(song.getDeveloperRate());
ImageD.setImageResource(song.getImage());
mediaplayer.start();
mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
IsPaused = true;
}
});
}
}
IsPaused = false;
} else if (mediaplayer != null) {
//If mediaPlayer is already Playing a song.
if (mediaplayer.isPlaying()) {
P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
mediaplayer.pause();
IVP_P.setImageResource(R.drawable.ic_play_arrow_black_24dp);
IsPaused = true;
}
}
//If mediaPlayer is used for first time and if mediaPlayer is neither paused
else {
if (mediaplayer != null) {
mediaplayer.release();
mediaplayer = null;
}
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mediaplayer = mediaplayer.create(SongsListActivity.this, song.getSong());
Old_Song = song.getSong();
NameD.setText(song.getNameOfSong());
RateD.setText(song.getDeveloperRate());
ImageD.setImageResource(song.getImage());
mediaplayer.start();
P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);
mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
IsPaused = true;
P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);
}
});
}
}
}
}
);
现在,每当执行进入任何onCompletion()方法时,我都希望执行从onItemClick()方法的开头开始执行。
我该怎么办?
更详细: -
这里我的应用程序使用ListView和Adapter显示歌曲列表。歌曲的细节存储在一个arrayList中(如你所能看到的)。当一个项目被点击时,它获取该项目的位置,然后引用arrayList的相应元素,获取相应歌曲的位置然后播放那首歌。我想要的是,当一首歌结束,并调用onCompletion()方法时,我想增加i的值(在我的代码的第4行看一下),然后转到onItemClick的第一行方法
答案 0 :(得分:0)
你所谈论的内容被称为GOTO,被广泛认为是糟糕的设计。相反,将要调用的代码提取到方法中,并从要执行它的两个(所有)位置调用它。