-1

我尝试从可变列表中获取列表并将其解析为数组成为微调器数组,这是我的代码;

fun getListLeague(context: Context): MutableList<League> {
        val league: MutableList<League> = mutableListOf()
        val leagueName = context.resources.getStringArray(R.array.leagueName)
        val leagueId = context.resources.getStringArray(R.array.leagueId)

        league.clear()

        for (i in leagueName.indices) {
            league.add(League(leagueName[i], leagueId[i]))
        }

        return league
    }

然后我在片段中的微调器中调用它,这是代码;

private lateinit var leagues: List<League>

    leagues = getListLeague(requireContext())
    val spinAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, leagues)

    Log.e("LIST LEAGUE PREV", leagues.toString())

我尝试从上面的 Log 中查看列表,我希望结果如下所示;

E/LIST LEAGUE PREV: English Premier League, English League Championship, Scottish Premier League, German Bundesliga, Italian Serie A, French Ligue 1, Spanish La Liga, so on :)

然后我尝试运行该应用程序,但日志结果如下所示:

E/LIST LEAGUE PREV: [League(leagueName=English Premier League, leagueId=4328), League(leagueName=English League Championship, leagueId=4329), League(leagueName=Scottish Premier League, leagueId=4330), League(leagueName=German Bundesliga, leagueId=4331), League(leagueName=Italian Serie A, leagueId=4332), League(leagueName=French Ligue 1, leagueId=4334), League(leagueName=Spanish La Liga, leagueId=4335), League(leagueName=Greek Superleague Greece, leagueId=4336), League(leagueName=Dutch Eredivisie, leagueId=4337), League(leagueName=Belgian Jupiler League, leagueId=4338), League(leagueName=Turkish Super Lig, leagueId=4339), League(leagueName=Danish Superliga, leagueId=4340), League(leagueName=Portuguese Primeira Liga, leagueId=4344), League(leagueName=American Major League Soccer, leagueId=4346), League(leagueName=Swedish Allsvenskan, leagueId=4347), League(leagueName=Mexican Primera League, leagueId=4350), League(leagueName=Brazilian Brasileirao, leagueId=4351), League(leagueName=Ukrainian Premier League, leagueId=4354), League(leagueName=Russian Football Premier League, leagueId=4355), League(leagueName=Australian A-League, leagueId=4356), League(leagueName=Eliteserien, leagueId=4358), League(leagueName=Chinese Super League, leagueId=4359)]

结果出乎我的意料,那我哪里做错了?

4

1 回答 1

1

您在 getListLeague 中同时获得了 LeagueID 和 LeagueName

val leagueId = context.resources.getStringArray(R.array.leagueId)

league.add(League(leagueName[i], leagueId[i]))

你正在返回这两个属性,

return league

这就是您在日志中获取 LeagueID 的原因。

其次,你打电话给这个

leagues = getListLeague(requireContext())
Log.e("LIST LEAGUE PREV", leagues.toString())

所以你得到你的结果

League(leagueName=English Premier League

我的建议是在更改代码之前先了解您在做什么。

于 2018-11-02T03:31:19.803 回答