简体   繁体   English

在实时数据 Kotlin 中更改数据

[英]Changing data in Live Data Kotlin

I have allRecords - Live Data values obtained from the Room, through the Repository.我有allRecords - 通过存储库从房间获得的实时数据值。

I want the handleSelectedItem method to change the values of one item in the LiveData<List<...>> if it is matched by id.如果LiveData<List<...>>与 id 匹配,我希望handleSelectedItem方法更改其中一项的值。 I tried to do this with Transformation.map (), but this code does not work我试图用 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)
                }
            }
        }
    }
}

Help to solve this problem帮助解决这个问题

Update Here offered MutableLiveData instead of LiveData .更新这里提供了MutableLiveData而不是LiveData Then both Repository and Dao should return MutableLiveData然后 Repository 和 Dao 都应该返回 MutableLiveData

Repository存储库

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

Dao

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

But Room database cannot return MutableLiveData但 Room 数据库无法返回 MutableLiveData

Error错误

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();

Update2更新2

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

Error addSource()错误添加源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

Your LiveData objects should be val .您的LiveData对象应该是val Use MutableLiveData / MediatorLiveData and setValue or postValue to change the value in LiveData使用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) }
        }
    }
}

Then both Repository and Dao should return MutableLiveData然后 Repository 和 Dao 都应该返回 MutableLiveData

No, it shouldn't.不,不应该。 Use LiveData in your DAO and Repository在您的DAORepository使用LiveData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM