简体   繁体   English

ViewModel,LiveData和Transformations.map

[英]ViewModel, LiveData and Transformations.map

New to Kotlin and Android development and related and I'm not certain what to do to apply some business logic and convert a value. Kotlin和Android开发的新手和相关人员,我不确定该如何应用一些业务逻辑并转换值。 I have a List of a class and I'd like to modify one of the values in the class while leaving everything else in the class unharmed. 我有一个类的列表,我想修改该类中的一个值,同时不影响该类中的其他所有内容。 Once I get to the view model, I'm not certain of how to access the time value in my class to modify it. 一旦进入视图模型,我就不确定如何访问类中的时间值以对其进行修改。 I'd appreciate if someone would point me in the right direction. 如果有人能指出我正确的方向,我将不胜感激。

Entity and Dao 实体与道

import org.threeten.bp.Instant

data class ActionDetails(val time: Instant,
                     val firstName: String,
                     ... )

@Query("SELECT time, first_name as firstName...")
fun liveStatus(): LiveData<List<ActionDetails>>

ViewModel 视图模型

class MainViewModel(private val repository: DataRepository) : ViewModel() {

    private val _actions: LiveData<List<ActionDetails>>
    val actions: LiveData<List<ActionDetails>>
        get() = _actions

    init {
        _actions = Transformations.map(repository.liveStatus()) {
            //Convert Instant value per business rules and convert to formatted string
            time -> ...

        }
    }
}

You can't change just 1 value. 您不能只更改1个值。 Create a new data class that represents the desired full object after the business logic transformation 在业务逻辑转换之后,创建一个新数据类来表示所需的完整对象

eg from 例如来自

data class ActionDetails(val time: Date, val firstName: String, val lastName: String)

to

data class DisplayItem(val time: String, val firstName: String, val lastName: String, val fullName: String)

Then transform your livedata from List<ActionDetails> to List<DisplayItem> . 然后将实时数据从List<ActionDetails>List<DisplayItem> To do that you can use the Iterable#map function that applies a transformation to each element of the list and returns the resulting list. 为此,您可以使用Iterable#map函数,该函数将转换应用于列表的每个元素并返回结果列表。

Transformations.map(repository.liveStatus()) { list ->
    list.map { item ->
        val formattedTime = item.time.toString() // whatever you need
        val fullName = "${item.firstName} ${item.lastName}"
        DisplayItem(formattedTime, item.firstName, item.lastName, fullName)
    }
}

Sidenote: if it's just 1 small thing that you want to change, maybe don't use Transformations.map but simply format the string at the place where it's displayed, eg in a adapter view holder 旁注:如果仅是您要更改的一件小事,也许不要使用Transformations.map而只需在显示位置(例如在适配器视图支架中)格式化字符串

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

相关问题 Transformations.map LiveData - Transformations.map LiveData 可以在ViewModel内部由Transformations.map()返回的Livedata上调用observeForever()吗? - It is ok to call observeForever() on Livedata returned by Transformations.map() inside ViewModel? 使用 Transformations.map 在 ViewModel 中处理 Room 数据库中的 LiveData - Processing LiveData from Room database in ViewModel with Transformations.map 具有多个参数的 LiveData Transformations.map() - LiveData Transformations.map() with multiple arguments LiveData 的 Transformations.map 未在单元测试中触发,并且仅在单元测试中触发 - LiveData's Transformations.map not being triggered in, and only in, Unit Tests LiveData,Transformations.map()-用户更改过滤顺序时如何强制刷新? - LiveData, Transformations.map() - How to force to refresh when the user changes filtering order? 使用 Transformations.map 替代 observeForever - Alternative for observeForever using Transformations.map Transformations.map() 返回 null 值 - Transformations.map() returns null values 我可以在 Kotlin 中用 Transformations.map 替换 _task.map 吗? - Can I replace _task.map with Transformations.map in Kotlin? 我对 Transformations.map 的绑定似乎不起作用 - My binding to a Transformations.map doesn't seem to be working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM