Consider this Kotlin Compose code snippet using LazyColumn:
LazyColumn {
items(listOf("Apple", "Banana", "Cherry")) { fruit ->
Text(text = fruit)
}
}What will the user see on the screen?
LazyColumn {
items(listOf("Apple", "Banana", "Cherry")) { fruit ->
Text(text = fruit)
}
}Remember, LazyColumn creates a vertical list by default.
The LazyColumn displays items vertically. Each Text composable shows one fruit on its own line, so the user sees Apple, Banana, and Cherry stacked vertically.
Which of these Kotlin Compose snippets correctly compiles and displays a list of numbers 1 to 3?
Check the function name and parameter types carefully.
Option B correctly uses items with a list and converts the number to string for Text. Option B uses item which expects a single item, not a list. Option B passes a range but items expects a collection. Option B passes an integer directly to Text which expects a string.
Given this code:
val fruits = remember { mutableStateListOf("Apple", "Banana") }
LazyColumn {
items(fruits) { fruit ->
Text(text = fruit)
}
}
// Later fruits.add("Cherry") is calledWhat will happen in the UI after adding "Cherry" to fruits?
val fruits = remember { mutableStateListOf("Apple", "Banana") }
LazyColumn {
items(fruits) { fruit ->
Text(text = fruit)
}
}
// fruits.add("Cherry") called laterThink about how Compose tracks changes in mutableStateListOf.
Using mutableStateListOf makes the list observable. When an item is added, Compose automatically recomposes the LazyColumn to show the new item.
You want to show a list of names in a LazyColumn. When a user taps a name, you want to navigate to a detail screen with that name.
Which code snippet correctly handles item clicks?
Remember how to add click behavior to composables using modifiers.
Option A correctly adds a clickable modifier to each Text item. Option A tries to add clickable outside items which is invalid. Option A applies clickable modifier outside composable tree. Option A uses an invalid onClick parameter on Text which does not exist.
Examine this code:
val items = listOf("One", "Two", "Three")
LazyColumn {
items(items.size) { index ->
Text(text = items[index])
}
}When running, the app crashes with an IndexOutOfBoundsException. Why?
val items = listOf("One", "Two", "Three") LazyColumn { items(items.size) { index -> Text(text = items[index]) } }
Check the expected parameter types for items in LazyColumn.
The items function expects a collection or a list, not an integer size. Passing items.size (an Int) causes the function to treat it as a count, but the lambda parameter is the item, not the index. Accessing items[index] then causes an index error.