【问题标题】:How to transform a LiveData<List<X>> in LiveData<PagedList<X>>?如何在 LiveData<PagedList<X>> 中转换 LiveData<List<X>>?
【发布时间】:2019-07-09 10:23:53
【问题描述】:

有什么方法可以将LiveData&lt;List&lt;X&gt;&gt; 类型的对象转换为LiveData&lt;PagedList&lt;X&gt;&gt; 类型的对象?

【问题讨论】:

    标签: android android-livedata


    【解决方案1】:

    据我所知,您可以通过以下方式做到这一点:

        class FirstType
        class SecondType
    
        val initType: LiveData<FirstType> = MutableLiveData<FirstType>()
        val resultType : LiveData<SecondType> = Transformations.map(initType, ::convertTypes)
    
        fun convertTypes(firstType: FirstType) : SecondType = SecondType()
    

    更新:
    List&lt;T&gt;转换成PagedList&lt;T&gt;试试看:
    How to convert a List<Object> to PagedList<Object> and vice-versa?

    【讨论】:

    • 这不是将类型从FirstType 更改为SecondType。类型相同X。这是关于将LiveData&lt;PagedList&lt;X&gt;&gt; 中的LiveData&lt;List&lt;X&gt;&gt; 转换为PagedList&lt;X&gt;,参见List&lt;X&gt;。有什么办法可以做到吗?
    • 感谢更新,但snapshot()PagedList&lt;Object&gt; 转换为List&lt;Object&gt; ,反之亦然。有可能吗?
    • 在来自链接的回答中,经过snapshot()的解释,从Now as for the other way around...开始不是你需要的吗?
    • 让我再检查一下,我会回复你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多