方法在Arraylist中跳过一个

时间:2011-11-23 17:19:42

标签: android loops if-statement for-loop arraylist

我有一个方法,每次用户点击它转到下一首歌。这是有效的,直到它到达最后一个,然后我得到一个力量关闭我希望媒体播放器停止,当它到达最后一个而不是强制关闭

ArrayList<String> nextSongname = nextsonginfo.getStringArrayList("nextsong");
ArrayList<String> nextArtistname = nextsonginfo.getStringArrayList("nextartist");
ArrayList<String> nextAlbumname = nextsonginfo.getStringArrayList("nextalbum");
ArrayList<String> nextPathname = nextsonginfo.getStringArrayList("nextpath");
ArrayList<String> artworkkeyword = nextsonginfo.getStringArrayList("nextartwork");
int index = nextSongname.indexOf(nextSongname.get(IplayerActivity.pos-2));
Uri newuri = Uri.parse(artworkkeyword.get(index+counter));

songname.setText(nextSongname.get(index+counter));
artistname.setText(nextArtistname.get(index+counter));
albumname.setText(nextAlbumname.get(index+counter));
path = nextPathname.get(index+counter);
artworkimageview.setImageURI(newuri);

try{
    playAudio(path);
}catch(Exception e ){
    e.getStackTrace();
}

if(j < nextPathname.size()-1){
    counter+=1;
}else{
    index = 0;
    counter = 0;
    j = 0;
}
j++;

1 个答案:

答案 0 :(得分:0)

我建议在try catch中移动休闲代码:

songname.setText(nextSongname.get(index+counter));
artistname.setText(nextArtistname.get(index+counter));
albumname.setText(nextAlbumname.get(index+counter));
path = nextPathname.get(index+counter);
artworkimageview.setImageURI(newuri);

...你可能会得到:索引超出范围异常。

其他推荐是在播放音频之前检查空格的路径或null。 (C#中的样本)

if(!string.IsNullOrWhiteSpace(path))
    playAudio(path);

注意:j未使用。