从 Jetpack Compose 中的 api 获取数据

问题描述 投票:0回答:1

我从 Jetpack Compose 中的 api 获取数据,但是当我尝试在 LazyRow 中显示它时,没有显示任何内容,而当我手动输入它时,它工作正常。

             LazyRow(
                        contentPadding = PaddingValues(horizontal = 20.dp),
                        horizontalArrangement = Arrangement.spacedBy(15.dp)
                    ) {
                        professorViewModel.getByUniversityName("universityName") { response ->
                            if (response.status == "OK") {
                                val professorList = response.data!!
                                //During debugging, I see that this list
                                //is correctly filled with two items
                                //and the information is taken from the api and its status is ok.
                                items(professorList.size) {
                                    val professor = professorList[0]
                                    ProfessorItem(professor) //function to show each item
                                }
                            }
                        }
                    }
android kotlin android-jetpack-compose retrofit lazy-loading
1个回答
0
投票

这将只显示第一件事,因为它总是显示 index 0 的元素。 更好的方法是:

LazyRow(
         contentPadding = PaddingValues(horizontal = 20.dp),
           horizontalArrangement = Arrangement.spacedBy(15.dp)
                    ) {                      professorViewModel.getByUniversityName("universityName") { response ->
                            if (response.status == "OK") {
                                val professorList = response.data!!
                                //During debugging, I see that this list
                                //is correctly filled with two items
                                //and the information is taken from the api and its status is ok.
                                items(professorList) {professor->  
                            
                                   ProfessorItem(professor) 
                                }
                            }
                        }
                    }

确保您导入正确的

items
函数,因为有两个函数采用
Int
作为参数,另一个函数采用
List
作为参数,因此您导入最后一个

© www.soinside.com 2019 - 2024. All rights reserved.