gpt4 book ai didi

android - Android MVVM + Room通过其他LiveData对象创建LiveData RecyclerViewItem对象

转载 作者:行者123 更新时间:2023-12-02 13:25:30 24 4
gpt4 key购买 nike

我有 session 室实体类“症状”,其名称为“症状”及其ID。

@Entity(tableName = "symptoms")
data class Symptom(
@PrimaryKey @NonNull val id: Int,
val name: String) {

override fun toString(): String {
return "Symptom $id: $name"
}
}
我在以下类(class)中得到它:
症状道
@Dao
interface SymptomDao {

@Query("SELECT * FROM symptoms WHERE id=:id LIMIT 1")
fun getSymptom(id: Int): Symptom

@Query("SELECT * FROM symptoms")
fun getAllSymptoms(): LiveData<List<Symptom>>
}
症状存储库
class SymptomRepository(private val symptomDao: SymptomDao) {

fun getSymptom(id: Int) = symptomDao.getSymptom(id)

fun getAllSymptoms() = symptomDao.getAllSymptoms()
}
症状ViewModel
class SymptomsViewModel(symptomRepository: SymptomRepository): ViewModel() {

private val symptomsList = symptomRepository.getAllSymptoms()
private val symptomsItemsList: MutableLiveData<List<SymptomItem>> = MutableLiveData()

fun getAllSymptoms(): LiveData<List<Symptom>> {
return symptomsList
}

fun getAllSymptomsItems(): LiveData<List<SymptomItem>> {
return symptomsItemsList
}
}
我的RecyclerView带有 SymptomItem 列表和复选框,以记住用户选择列表的哪些症状:
data class SymptomItem(
val symptom: Symptom,
var checked: Boolean = false)
问题
我的问题是如何通过 LiveData<List<SymptomItem>>获得 LiveData<List<Symptom>>?我刚刚开始学习MVVM,但找不到简单的答案来做到这一点。我已经尝试过以各种方式填写此列表,但是每次旋转手机时,它都会丢失 checked变量。如有任何提示,我将不胜感激。

最佳答案

您需要通过将其ID存储在ViewModel的列表中来存储要检查的项目。然后,您将合并Symptom对象的列表和检查哪些项目的列表,并生成SymptomItem对象的列表。
我将使用Kotlin Flow来实现这一目标。

@Dao
interface SymptomDao {

@Query("SELECT * FROM symptoms")
fun flowAllSymptoms(): Flow<List<Symptom>>
}
class SymptomRepository(private val symptomDao: SymptomDao) {

fun flowAllSymptoms() = symptomDao.flowAllSymptoms()
}
class SymptomsViewModel(
private val symptomRepository: SymptomRepository
) : ViewModel() {

private val symptomsListFlow = symptomRepository.flowAllSymptoms()

private val symptomsItemsList: MutableLiveData<List<SymptomItem>> = MutableLiveData()

private var checkedIdsFlow = MutableStateFlow(emptyList<Int>())

init {
viewModelScope.launch {
collectSymptomsItems()
}
}

private suspend fun collectSymptomsItems() =
flowSymptomsItems().collect { symptomsItems ->
symptomsItemsList.postValue(symptomsItems)
}

private fun flowSymptomsItems() =
symptomsListFlow
.combine(checkedIdsFlow) { list, checkedIds ->
list.map { SymptomItem(it, checkedIds.contains(it.id)) }
}

fun checkItem(id: Int) {
(checkedIdsFlow.value as MutableList<Int>).add(id)
checkedIdsFlow.value = checkedIdsFlow.value
}

fun uncheckItem(id: Int) {
(checkedIdsFlow.value as MutableList<Int>).remove(id)
checkedIdsFlow.value = checkedIdsFlow.value
}

fun getSymptomsItems(): LiveData<List<SymptomItem>> {
return symptomsItemsList
}
}
在您的片段中,观察 getSymptomsItems()并更新您的适配器数据。
该代码未经测试,您可能需要进行一些小的调整才能编译。

关于android - Android MVVM + Room通过其他LiveData对象创建LiveData RecyclerViewItem对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64412203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com