【问题标题】:Android: how to get index of ArrayList row with custom objectAndroid:如何使用自定义对象获取 ArrayList 行的索引
【发布时间】:2019-04-07 00:15:31
【问题描述】:

我有这样的歌曲 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】:
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++;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多