I have a list of items where each has a checkbox. The state of the checkbox needs to be stored in the viewmodel
and preferable the list in the viewmodel
should be the only source of truth.
@Composable
fun Screen(viewModel: ViewModel) {
val list by viewModel.items.observeAsState()
LazyColumn {
list?.let { items ->
items(items) { item ->
ListItem(
text = {
Text(item.name)
},
secondaryText = {
Text(item.phoneNumber)
},
icon = {
Checkbox(
modifier = Modifier.padding(8.dp),
checked = item.selected,
onCheckedChange = {
//TODO
}
)
}
)
}
}
}
}
I have tried to update the item.selected
by updating the list (MutableLiveData<List<Object>>
in viewmodel
) in the onCheckedChange
callback, but the UI does not update. If i scroll down and then up the UI updates and the checkbox appears to be checked. Why does it not update?