更改实时数据Kotlin中的数据

时间:2019-12-12 15:54:30

标签: android kotlin

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

我希望handleSelectedItem方法更改LiveData<List<...>>中一项与ID匹配的值。 我尝试使用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()

Dao

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

Update2

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

1 个答案:

答案 0 :(得分:1)

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

然后存储库和Dao都应返回MutableLiveData

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