我有allRecords-通过存储库从 session 室获得的实时数据值。

我希望handleSelectedItem方法更改与ID匹配的LiveData<List<...>>中一项的值。
我尝试使用Transformation.map()完成此操作,但是此代码不起作用

class RecordListViewModel @Inject constructor(val repository: RecordRepository): ViewModel() {
    private var allRecords : LiveData<List<RecordItem>> = Transformations.map(repository.getAllRecording()){
        records -> return@map records.map{ it.toItem()}

    }



    fun getAllRecords() : LiveData<List<RecordItem>>{
        return allRecords
    }

    fun handleSelectedItem(id : Int) {
        Log.d("HandleSelectedItem", "Test1")
        allRecords = Transformations.map(allRecords) { records ->
            return@map records.map {
                if (it.id == id){
                    Log.d("HandleSelectedItem", "Test2")
                    it.copy(isSelected = true)
                }
                else{
                    Log.d("HandleSelectedItem", "Test3")
                    it.copy(isSelected = false)
                }
            }
        }
    }
}

帮助解决这个问题

更新
这里提供MutableLiveData而不是LiveData
然后存储库和Dao都应返回MutableLiveData

存储库
fun getAllRecording(): MutableLiveData<List<RecordEntity>> =
        appDatabase.getRecordDao().getAllRecording()


@Query("SELECT * FROM record")
fun getAllRecording() : MutableLiveData<List<RecordEntity>>

但是Room数据库无法返回MutableLiveData

错误
D:\Project\VoiceRecording\app\build\tmp\kapt3\stubs\debug\ru\ddstudio\voicerecording\data\database\daos\RecordDao.java:17: error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.MutableLiveData<java.util.List<ru.ddstudio.voicerecording.data.database.entities.RecordEntity>>).
    public abstract androidx.lifecycle.MutableLiveData<java.util.List<ru.ddstudio.voicerecording.data.database.entities.RecordEntity>> getAllRecording();

更新2
private val allRecords = MediatorLiveData<List<RecordItem>>().apply {
            val recordsRepository = repository.getAllRecording().map { records -> records.map { it.toItem() } }
            addSource(recordsRepository)
        }

错误addSource()
None of the following functions can be called with the arguments supplied:
@MainThread public final fun <S : Any!> addSource(@NonNull p0: LiveData<List<RecordItem>!>, @NonNull p1: (List<RecordItem>!) -> Unit): Unit defined in androidx.lifecycle.MediatorLiveData
@MainThread public open fun <S : Any!> addSource(@NonNull p0: LiveData<List<RecordItem>!>, @NonNull p1: Observer<in List<RecordItem>!>): Unit defined in androidx.lifecycle.MediatorLiveData

最佳答案

您的LiveData对象应该是val。使用MutableLiveData / MediatorLiveDatasetValuepostValue更改LiveData中的值

class RecordListViewModel @Inject constructor(val repository: RecordRepository): ViewModel() {

    private val allRecords = MediatorLiveData<List<RecordItem>>().apply {
        val recordsLiveData = repository.getAllRecording().map { records -> records.map { it.toItem() } }
        addSource(recordsLiveData) { records ->
            value = records
        }
    }

    fun getAllRecords() : LiveData<List<RecordItem>> {
        return allRecords
    }

    fun handleSelectedItem(id : Int) {
        Log.d("HandleSelectedItem", "Test1")
        allRecords.value?.let { records ->
            allRecords.value = records.map { it.copy(isSelected = it.id == id) }
        }
    }
}

不,不应该。在LiveDataDAO中使用Repository

10-07 23:35