提问人:Jonas 提问时间:10/28/2023 更新时间:10/28/2023 访问量:26
会议室数据库更新列 (CHeckbox)
Room Database Update a column (CHeckbox)
问:
我最近开始编写应用程序,但还没有那么多经验。尤其是数据库编程。
我有购物清单,想检查商品。
我仍然无法更新该列。我在互联网上找到了一些东西,但无法将它们放在一起。例如,我不知道如何从项目中获取 id。或者我根本不需要身份证件?
我寻求帮助。谢谢!
实体:
@Entity
data class ShoppingList(
val Name: String,
val Einheit: String,
val Menge: String,
@ColumnInfo(name = "isChecked", defaultValue = "0")
val isChecked: Boolean,
@PrimaryKey(autoGenerate = true)
val id: Int = 0
道:
@Dao
interface ShoppingListDao {
@Upsert
suspend fun upsertItem(items: ShoppingList)
@Query("UPDATE ShoppingList SET isChecked = :isChecked WHERE id = :id")
suspend fun updateItem(isChecked: Boolean, id: Int)
@Delete
suspend fun deleteItem(items: ShoppingList)
@Query("SELECT * FROM ShoppingList ORDER BY Name ASC")
fun getItemsOrderByName(): Flow<List<ShoppingList>>
}
事件:
sealed interface ShoppingListEvent{
object SaveItem: ShoppingListEvent
data class SetName(val Name: String): ShoppingListEvent
data class SetEinheit(val Einheit: String): ShoppingListEvent
data class SetMenge(val Menge: String): ShoppingListEvent
data class SetCheckbox(val isChecked: Boolean): ShoppingListEvent
object ShowDialog: ShoppingListEvent
object HideDialog: ShoppingListEvent
data class SortItems(val sortType: SortType): ShoppingListEvent
data class DeleteItem(val items: ShoppingList): ShoppingListEvent
}
州:
data class ShoppingListState (
val items: List<ShoppingList> = emptyList(),
val Name: String = "",
val Einheit: String = "",
val Menge: String = "",
val isChecked: Boolean = false,
val isAddingItem: Boolean = false,
val sortType: SortType = SortType.Name
)
视图模型:
class ShoppingListViewModel(
private val dao: ShoppingListDao
): ViewModel() {
private val _sortType = MutableStateFlow(SortType.Name)
private val _items = _sortType
.flatMapLatest { sortType ->
when(sortType) {
SortType.Name -> dao.getItemsOrderByName()
}
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), emptyList())
private val _state = MutableStateFlow(ShoppingListState())
val state = combine(_state, _sortType, _items) { state, sortType, items ->
state.copy(
items = items,
sortType = sortType
)
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), ShoppingListState())
fun onEvent(event: ShoppingListEvent){
when(event){
....
is ShoppingListEvent.SetCheckbox -> {
//I don't know what to do here.
val isChecked = state.value.isChecked
viewModelScope.launch {
dao.updateItem(isChecked)
}
...
}
}
}
}
和复选框:
Checkbox(
checked = state.isChecked,
onCheckedChange ={
onEvent(ShoppingListEvent.SetCheckbox(it))
}
)
我尝试了几件事。有时什么也没发生,有时所有项目都被检查了。
答:
1赞
amtrax
10/28/2023
#1
如果我理解正确的话,有项目列表,每个项目都有复选框。如果是这种情况,那么复选框的状态应该取决于它所属的项目。
Checkbox(
checked = state.items[itemIndex].isChecked,
onCheckedChange ={
onEvent(ShoppingListEvent.SetCheckbox(it,
!state.items[itemIndex].isChecked))
}
)
若要更新单个项目的状态,您需要传递正在检查的项目的 ID。
fun onEvent(event: ShoppingListEvent, itemId : Int){
when(event){ ...
is ShoppingListEvent.SetCheckbox -> {
viewModelScope.launch {
dao.updateItem(isChecked, itemID)
}
...
}
}
评论
0赞
Jonas
10/28/2023
现在出现了另一个问题。我使用导航,并收到以下错误:“ 类型不匹配:推断类型是 KFunction2<ShoppingListEvent, Int, Unit> but (ShoppingListEvent) -> Unit was expected” MainActivity: 导航: Viewmodel:Navigation(state = state, onEvent = viewModel::onEvent)
fun Navigation( state: ShoppingListState, onEvent: (ShoppingListEvent) -> Unit ){ .. composable(route = Screen.MainScreen.route){ MainScreen(navController = navController, state = state, onEvent = onEvent) }
fun onEvent(event: ShoppingListEvent, id: Int){
0赞
Jonas
10/28/2023
我看到导航中的类型不匹配,但我不知道正确的类型是什么。
0赞
amtrax
10/29/2023
您需要更改函数定义,因为我们在 ViewModel 中更改了一个函数定义。onEvent: (ShoppingListEvent, Int) -> 单位 )
下一个:SQL 更新集大小写时 else
评论