房间数据库更新列(复选框)

问题描述 投票:0回答:1

我最近开始编写应用程序,但还没有那么多经验。尤其是数据库编程。

我有一个购物清单,想要检查商品。

我仍然无法更新专栏。我在互联网上找到了一些东西,但无法将它们放在一起。例如,我不知道如何从项目中获取 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))
    }
)

我尝试了一些事情。有时什么也没有发生,有时所有项目都被检查了。

database kotlin checkbox sql-update android-room
1个回答
0
投票

如果我理解正确的话,有一个项目列表,每个项目都有复选框。如果是这种情况,那么复选框的状态应该取决于它所属的项目。

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

        ...

        }
        
    }
© www.soinside.com 2019 - 2024. All rights reserved.