Android:如何使用自定义对象获取ArrayList行的索引

问题描述 投票:-1回答:1

我有像这样的歌曲ArrayList

songs.add(new Songs("Rumour Has It","Adele", "Adele21", R.drawable.adele, 1991, R.drawable.adele21_cover));

songs.add(new Songs("Turning Tables","Adele", "Adele21", R.drawable.adele, 1991, R.drawable.adele21_cover));

songs.add(new Songs("dangerous","M Jackson", "Dangerous", R.drawable.jackson, 1992, R.drawable.dangerous_cover));

我想获得ArrayList行的索引,其中包含正在播放的歌曲“Turning Tables”,因此我可以播放下一首或上一首歌曲

String songName = preferenceConfig.readSongName();
for(Songs s : MainActivity.songs){
    boolean resultOfComparison= songName.equals(s.getSong());
    if(resultOfComparison){
       //how to get the index of this row which contains this song
    }
 }
java android arraylist indexof
1个回答
0
投票
for(Songs s : MainActivity.songs) {
     boolean resultOfComparison= songName.equals(s.getSong());
     if(resultOfComparison){
        Toast.makeText(context, "Current index is: " + (songIndex), Toast.LENGTH_SHORT).show();
        }
     songIndex++;
}
© www.soinside.com 2019 - 2024. All rights reserved.