Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

View API games grouped by league id only got first league with itemIndexed

$
0
0

I'm trying to get matches by date from a football API and show them grouped by league.So, I've made a mutable list in ViewModel to store every match item with the fixtureId as its unique identifier. The problem is that items are added to the list in every call from the API without overwriting the old ones, so the list keeps growing in every call, and only one league (which contains two games that day) is shown with an infinite repetition. When changing the date, the new data is added to the list along with the old ones. I want to access the results in the following image.

sample

If anyone can help, thank you.

@Composablefun 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

@HiltViewModelclass 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)

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>