Issue
I'm try to use a string array to populate my LazyColumn
but every time I run my app it appears blank. Did I miss something out?
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)
}
}
}
Solution
Is this not suitable for you?
@Composable
fun MainScreen(itemArray: Array<out String>) {
val listState = rememberLazyListState()
LazyColumn(state = listState) {
items(itemArray){ item ->
Text(text = item)
}
}
}
Answered By - commandiron
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.