我编写了硬编码的 ToDo 元素列表,并制作了 2 个可组合函数来显示数据。
@Composable
fun TodoAppContent() {
val todos = remember { (DataProvider.listOfToDoEntries) }
Column(
modifier = Modifier
.fillMaxSize()
.background(Color(0xff503d69))
) {
LazyColumn(
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp),
modifier = Modifier
.weight(1f)
) {
items(items = todos, itemContent = { CreateToDoListItem(todo = it) })
}
Button(
onClick = {},
modifier = Modifier
.size(60.dp)
.align(alignment = Alignment.CenterHorizontally),
colors = ButtonDefaults.buttonColors(backgroundColor = Color(0xffc1b6d1)),
shape = CircleShape
) {
Text(text = "+")
}
}
Run Code Online (Sandbox Code Playgroud)
}
@Composable
fun CreateToDoListItem(todo: …Run Code Online (Sandbox Code Playgroud) 我已经按照其他用户的建议实现了导入,androidx.compose.runtime.*但我仍然无法使用observeAsState来显示任何值,例如Text(text = weather.main.temp.toString())可组合元素内部的值。
可组合:
@Composable
fun WeatherAppHomeScreen(weatherViewModel: WeatherViewModel) {
val weather: Weather by weatherViewModel.myResponse.observeAsState()
Text(text = weather.main.temp.toString())
}
Run Code Online (Sandbox Code Playgroud)
数据类:
data class Weather(
@SerializedName("base")
val base: String,
@SerializedName("clouds")
val clouds: Clouds,
@SerializedName("cod")
val cod: Int,
@SerializedName("coord")
val coord: Coord,
@SerializedName("dt")
val dt: Int,
@SerializedName("id")
val id: Int,
@SerializedName("main")
val main: Main,
@SerializedName("name")
val name: String,
@SerializedName("rain")
val rain: Rain,
@SerializedName("sys")
val sys: Sys,
@SerializedName("timezone")
val timezone: Int,
@SerializedName("visibility")
val visibility: Int,
@SerializedName("weather")
val weather: List<WeatherX>,
@SerializedName("wind")
val …Run Code Online (Sandbox Code Playgroud)