我从 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
}
}
}
}
这将只显示第一件事,因为它总是显示 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
作为参数,因此您导入最后一个