Home > Back-end >  String array not appearing in LazyColumn
String array not appearing in LazyColumn

Time:04-26

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)
        }
    }
}

enter image description here

CodePudding user response:

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)
        }
    }
}
  • Related