Building a database with multiple tables. The tables are used to track component usage but I’m having a hard time getting a Query List to populate in a DropDownMenu. I want to limit the users to the List so my Foreign Key will work easier. When I have Log.d output what should be the List it always outputs a blank list []
. Hopefully the code below is enough to show my issue.
Dao:
@Query("SELECT bulletName FROM Bullet")
fun getBulletNames(): Flow<List<BulletNames>>
data classes:
data class BulletNames(
val bulletName: String,
)
data class AmmoState(
val bulletName: List<BulletNames> = emptyList(),
)
Event:
sealed interface AmmoEvent {
data object ShowDialog : AmmoEvent
}
ViewModel:
@OptIn(ExperimentalCoroutinesApi::class)
class AmmoViewModel(
private val dao: BBDao,
) : ViewModel() {
private val _ammoSortType = MutableStateFlow(AmmoSortType.AMMO_NAME)
private val _ammo = _ammoSortType
.flatMapLatest { sortType ->
when (sortType) {
AmmoSortType.AMMO_NAME -> dao.getAmmoOrderedByNameASC()
AmmoSortType.AMMO_QUANTITY -> dao.getAmmoOrderedByQuantityASC()
}
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), emptyList())
private val _state = MutableStateFlow(AmmoState())
val state = combine(_state, _ammoSortType, _ammo) { state, sortType, ammo ->
state.copy(
ammo = ammo,
sortType = sortType,
)
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), AmmoState())
fun onEvent(event: AmmoEvent) {
when (event) {
AmmoEvent.ShowDialog -> {
_state.update {
it.copy(
isAddingAmmo = true,
ammoBulletName = "",
)
}
viewModelScope.launch {
dao.getBulletNames()
//Log.d("list", _bulletOptions.toString())
}
}
}
}
}
DialogBox:
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AddAmmoDialog(
state: AmmoState,
onEvent: (AmmoEvent) -> Unit,
modifier: Modifier = Modifier,
) {
var expanded by remember { mutableStateOf(false) }
val bulletOptions = state.bulletName
var selectedBullet = ""
Log.d("List", bulletOptions.toString())
AlertDialog(
modifier = modifier,
onDismissRequest = { onEvent(AmmoEvent.HideDialog) },
confirmButton = {
Box(
modifier = Modifier.fillMaxWidth(),
contentAlignment = Alignment.CenterEnd
) {
Button(onClick = {
onEvent(AmmoEvent.SaveAmmo)
}) {
Text(text = "Save")
}
}
},
title = { Text(text = "Add Ammo") },
text = {
Column(
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
OutlinedTextField(
value = state.ammoName,
onValueChange = {
onEvent(AmmoEvent.SetAmmoName(it))
},
placeholder = { Text(text = "Ammo Name") },
label = { Text(text = "Ammo Name") },
)
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = { expanded = !expanded },
modifier = modifier,
) {
OutlinedTextField(
value = selectedBullet,
onValueChange = {},
label = { Text(text = "Select Bullet") },
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
modifier = Modifier
.menuAnchor()
.fillMaxWidth()
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
bulletOptions.forEach { item ->
DropdownMenuItem(
text = { Text(text = item.toString()) },
onClick = {
expanded = false
selectedBullet = item.toString()
onEvent(AmmoEvent.SetAmmoBulletName(item.toString()))
},
)
}
}
}
}
}
)
}
New contributor
Kurtis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.