如何从list< pair>获得列表在Android中
在我的应用程序中,我想要组合
两个延期列表,为此,我使用了kotlin flow
和combine
& ZIP
运营商!
我在下面编写代码,但是在活动中使用时,我不知道如何获取列表!
viewModel代码:
class ViewModel (private val repository: Repository) : ViewModel() {
val moviesFlow = repository.moviesList()
val genresFlow = repository.genresList()
fun loadData(): Flow<List<Pair<MoviesResponse.Data, GenresList>>> =
combine(moviesFlow, genresFlow) { movieList: Response<MoviesResponse>,
genreList: Response<GenresList> ->
val movies = movieList.body()?.data
val genres = genreList.body()!!
return@combine movies?.zip(genres)!!
}
}
活动代码:
lifecycleScope.launchWhenCreated {
viewModel.loadData().collect {
}
}
我想在列表中获取每个代码并设置为适配器
!
写入 收集,说我选择索引,例如: it [0] < /strong>
,
但我想获取每个列表并将其设置为回收器适配器
。
我该怎么办?
In my application I want combine
two deferent lists and for this I used kotlin flow
and combine
& zip
operators!
I write below codes, but when used in activity I don't know how can I get lists!
ViewModel codes :
class ViewModel (private val repository: Repository) : ViewModel() {
val moviesFlow = repository.moviesList()
val genresFlow = repository.genresList()
fun loadData(): Flow<List<Pair<MoviesResponse.Data, GenresList>>> =
combine(moviesFlow, genresFlow) { movieList: Response<MoviesResponse>,
genreList: Response<GenresList> ->
val movies = movieList.body()?.data
val genres = genreList.body()!!
return@combine movies?.zip(genres)!!
}
}
Activity codes :
lifecycleScope.launchWhenCreated {
viewModel.loadData().collect {
}
}
I want get each on lists and set into adapter
!
When write it into collect
, say me select index such as : it[0]
But I want get each lists and set into recycler adapter
.
How can I it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您的问题,您想将“对列表”转换为“对列表”。您可以为此使用
unzip
函数。在这里,
电影
将是Movieflow
和流派
的电影列表将从
genResflow
中成为流派列表。但是,如果您只想要这两个列表,则可以直接在
活动
中收集两个流,而不是第一次zipping然后解压缩。If I understand you question correctly, you want to convert "list of pairs" to "pair of lists". You can use the
unzip
function for this.Here,
movies
will be the list of movies frommoviesFlow
andgenres
willbe the list of genres from
genresFlow
.But if you only wanted these two lists separately, you could have collected the two flows in the
Activity
directly instead of first zipping and then unzipping.