Android LiveData Transformations

2020-11-22 22:58:52

参考地址 LiveData的数据转换 Transformations使用

有时候有这样的需求,需要在LiveData将变化的数据通知给观察者前,改变数据的类型;或者是返回一个不一样的LiveData。

这里介绍一个类Transformations,它可以帮助完成上面的这些操作。

Transformations.map()

//todo  LiveData创建方法1val liveData: MutableLiveData<NeiHanResult> = neiHanRepository.liveData
//todo LiveData创建方法2val liveData2 = Transformations.map(neiHanRepository.liveData) { it }!!Transformations.map()使用要修改后的数据对象val liveDataTemp = MutableLiveData<List<NeiHanResult2>>()//定义一个临时list存储对象var listData = mutableListOf<NeiHanResult2>()//todo  LiveData创建方法3 并修改数据val liveData3 = Transformations.map(neiHanRepository.liveData) {it.result.forEach { item ->val formattedTime = item.text + "66666"listData.add(NeiHanResult2(formattedTime, item.text))LogUtils.d(item.text)}//执行数据修改后 给新数据赋值liveDataTemp.value = listData}
//执行数据修改的方法neiHanViewModel.liveData3.observe(this, Observer {})//监听所需修改后数据neiHanViewModel.liveDataTemp.observe(this, Observer<List<NeiHanResult2>> {var text = ""it.forEach {text += "${it.name}:${it.text}\n"text_contact.text = text}})


  • 2018-03-08 09:44:15

    前端性能监控:window.performance

    Web Performance API允许网页访问某些函数来测量网页和Web应用程序的性能,包括 Navigation Timing API和高分辨率时间数据。

  • 2018-03-08 09:47:14

    ES6,Array.fill()函数的用法

    ES6为Array增加了fill()函数,使用制定的元素填充数组,其实就是用默认内容初始化数组。

  • 2018-03-08 09:53:39

    document.readyState

    一个document 的 Document.readyState 属性描述了文档的加载状态。

  • 2018-03-09 02:09:23

    ArrayBuffer:类型化数组

    ArrayBuffer对象、TypedArray对象、DataView对象是JavaScript操作二进制数据的一个接口。这些对象早就存在,属于独立的规格,ES6将它们纳入了ECMAScript规格,并且增加了新的方法。

  • 2018-03-09 11:45:11

    SQL SELECT DISTINCT 语句

    如需从 Company" 列中仅选取唯一不同的值,我们需要使用 SELECT DISTINCT 语句: