You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

足球API赛事按联赛分组异常:列表重复累加、单联赛重复展示

足球API赛事数据按日期获取并按联赛分组展示的问题与解决方案

问题描述

我尝试通过足球API按日期获取赛事数据,并按联赛分组展示。在ViewModel中创建了以fixtureId作为唯一标识的可变列表存储赛事项,但存在以下问题:

  • 每次API调用时新数据会被追加到列表中而非覆盖旧数据,导致列表持续增长,仅当日包含两场赛事的单个联赛被无限重复展示;
  • 切换日期时,新数据仍会与旧数据共存,无法达到预期展示效果。

原代码

Composable函数

@Composable
fun TodayMatchesLazy(
    matchesList: List<TodayResponseItem?>
) {
    val viewModel: MainViewModel = hiltViewModel()
    matchesList.forEach {
        viewModel.mainList
            .add(MatchesByLeague(
                leagueId = it!!.league!!.id!!,
                leagueName = it.league!!.name!!,
                leagueLogo = it.league.logo!!,
                matchId = it.fixture!!.id!!,
                teamHomeName = it.teams!!.home!!.name!!,
                teamHomeLogo = it.teams.home!!.logo!!,
                teamAwayName = it.teams.away!!.name!!,
                teamAwayLogo = it.teams.away.logo!!,
                teamHomeR = it.goals!!.home.toString(),
                teamAwayR = it.goals.away.toString(),
                time = it.fixture.date!!)
            )
    }
    val groupedList = viewModel.mainList.groupBy { it.leagueId }
    if (groupedList .isNotEmpty()) {
        LazyColumn(
            modifier = Modifier
                .padding(20.dp)
                .fillMaxSize()
        ) {
            groupedList .forEach { (league, items) ->
                item {
                    Row {
                        TodayMatchesHeader(leagueItem = matchesList, league = league)
                    }
                }
                itemsIndexed(items,
                    itemContent = { index, item ->
                        TodayMatchesRow(
                            teamHomeName = item.teamHomeName,
                            teamHomeLogo = item.teamHomeLogo,
                            teamHomeR = item.teamHomeR,
                            teamAwayName = item.teamAwayName,
                            teamAwayLogo = item.teamAwayLogo,
                            teamAwayR = item.teamAwayR,
                            time = item.time
                        )
                    }
                )
            }
        }
    }
}

ViewModel代码

@HiltViewModel
class MainViewModel @Inject constructor(private val liveMatchesRepository: MainRepository): ViewModel() {
    private var _mainList = mutableStateListOf<MatchesByLeague>()
    val mainList: MutableList<MatchesByLeague> = _mainList
}
data class MatchesByLeague(
    val leagueId: Int, val leagueName: String, val leagueLogo: String,
    val matchId: Int, val teamHomeName: String, val teamHomeLogo: String,
    val teamAwayName: String, val teamAwayLogo: String,
    val teamHomeR: String, val teamAwayR: String, val time: String
)

解决方案(更新后代码)

基于BenjyTec的解答,对代码进行修改后功能正常,更新后的代码如下:

Composable函数

@Composable
fun TodayMatchesLazy(
    matchesList: List<TodayResponseItem?>
) {
    val viewModel: MainViewModel = hiltViewModel()
    viewModel.mainList.value = matchesList
    val groupedList = viewModel.mainList.value.groupBy { it!!.league!!.id }
    if (groupedList.isNotEmpty()) {
        LazyColumn(
            modifier = Modifier
                .padding(20.dp)
                .fillMaxSize()
        ) {
            groupedList.forEach { (league, items) ->
                item {
                    TodayMatchesHeader(leagueItem = items[0]!!, league = league!!)
                }
                itemsIndexed(items,
                    itemContent = { index, item ->
                        TodayMatchesRow(
                            teamHomeName = item!!.teams!!.home!!.name!!,
                            teamHomeLogo = item.teams!!.home!!.logo!!,
                            teamHomeR = item!!.goals!!.home!!.toString(),
                            teamAwayName = item.teams.away!!.name!!,
                            teamAwayLogo = item.teams.away.logo!!,
                            teamAwayR = item.goals!!.away!!.toString(),
                            time = item.fixture!!.date!!
                        )
                    }
                )
            }
        }
    }
}

ViewModel代码

@HiltViewModel
class MainViewModel @Inject constructor(private val liveMatchesRepository: MainRepository): ViewModel() {
    private var _mainList = mutableStateOf<List<TodayResponseItem?>>(listOf())
    var mainList: MutableState<List<TodayResponseItem?>> = _mainList
}

内容的提问来源于stack exchange,提问作者Mahmoud Nabil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 07:05:23