字符串数组未出现在 LazyColumn 中
String array not appearing in LazyColumn
我尝试使用字符串数组来填充我的 LazyColumn
但每次我 运行 我的应用程序都显示为空白。我是不是漏掉了什么?
strings.xml
<resources>
<string name="app_name">Breads</string>
<!--Array (198 items)-->
<string-array name="array_breads">
<item>Aish merahrah</item>
...
<item>Zwieback</item>
</string-array>
</resources>
MainActivity.kt
class MainActivity : ComponentActivity() {
private var itemArray: Array<String>? = null
override fun onCreate(savedInstanceState: Bundle?) {
itemArray = resources.getStringArray(R.array.array_breads)
super.onCreate(savedInstanceState)
setContent {
AdaptiveTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
MainScreen(itemArray = itemArray as Array<out String>)
}
}
}
}
}
@Composable
fun MainScreen(itemArray: Array<out String>) {
val listState = rememberLazyListState()
LazyColumn (listState = listState) {
items(itemArray.size) {
stringArrayResource(id = R.array.array_breads)
}
}
}
这不适合你吗?
@Composable
fun MainScreen(itemArray: Array<out String>) {
val listState = rememberLazyListState()
LazyColumn(state = listState) {
items(itemArray){ item ->
Text(text = item)
}
}
}
我尝试使用字符串数组来填充我的 LazyColumn
但每次我 运行 我的应用程序都显示为空白。我是不是漏掉了什么?
strings.xml
<resources>
<string name="app_name">Breads</string>
<!--Array (198 items)-->
<string-array name="array_breads">
<item>Aish merahrah</item>
...
<item>Zwieback</item>
</string-array>
</resources>
MainActivity.kt
class MainActivity : ComponentActivity() {
private var itemArray: Array<String>? = null
override fun onCreate(savedInstanceState: Bundle?) {
itemArray = resources.getStringArray(R.array.array_breads)
super.onCreate(savedInstanceState)
setContent {
AdaptiveTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
MainScreen(itemArray = itemArray as Array<out String>)
}
}
}
}
}
@Composable
fun MainScreen(itemArray: Array<out String>) {
val listState = rememberLazyListState()
LazyColumn (listState = listState) {
items(itemArray.size) {
stringArrayResource(id = R.array.array_breads)
}
}
}
这不适合你吗?
@Composable
fun MainScreen(itemArray: Array<out String>) {
val listState = rememberLazyListState()
LazyColumn(state = listState) {
items(itemArray){ item ->
Text(text = item)
}
}
}