简体   繁体   English

将可变实时数据解析为 Kotlin 中的数组

[英]Parse Mutable Live Data to Array in Kotlin

There is a mutableLiveData Holding 2 array "deal" and "category" I need to parse this both in different adapters.有一个 mutableLiveData 持有 2 数组“交易”和“类别”,我需要在不同的适配器中解析它。

Is there a way I can convert 1 mutable live data to 2 array and then parse them to two different adapters有没有办法可以将 1 个可变实时数据转换为 2 个数组,然后将它们解析为两个不同的适配器

Suppose There is MutableVariable Name se假设有 MutableVariable Name se

private lateinit var mHomePojo: MutableLiveData<List<HomePojo>>

having parse Json as below解析 Json 如下

 {
   "status": 0,
   "response": "success",
   "category": [
     {
       "categoryName": "demo",
       "categoryDesc": "demo"
     },
     {
       "categoryName": "demo1",
       "categoryDesc": "demo"
     }
   ],
   "deal": [
     {
       "dealImg": "https://aiotechnology.in/Aditechweb/upload/153102117.jpg",
       "dealDesc": "gd",
       "dealStartDate": "2019-10-18",
       "dealEndDate": "2019-10-19"
     }
   ]
 }

Is there any way to parse private lateinit var mHomePojo: MutableLiveData<List<HomePojo>> to lateinit var mDealModel: MutableLiveData<List<DealModel>> and lateinit var mCategoryModel: MutableLiveData<List<CategoryModel>>有什么方法可以将private lateinit var mHomePojo: MutableLiveData<List<HomePojo>>解析为lateinit var mDealModel: MutableLiveData<List<DealModel>>lateinit var mCategoryModel: MutableLiveData<List<CategoryModel>>

I am new to MVVM please help我是 MVVM 新手,请帮忙

I think Transformations might be able to help you separate your home live data to two separate livedata object with specified properties.我认为转换可能能够帮助您将家庭实时数据分离为具有指定属性的两个单独的实时数据 object。 below is piece of code for this.下面是一段代码。 (NOTE: not used lateinit var for example) (注意:例如不使用 lateinit var)

private val homeLiveData: LiveData<HomePojo> = MutableLiveData<HomePojo>()

//Category Live data
private val categoryPojo = Transformations.map(homeLiveData) {
    it.category
}

//Deal live data
private val dealPojo = Transformations.map(homeLiveData) {
    it.deal
}

data class HomePojo(
   /*-- Other fields --*/
   val category: List<CategoryPojo>? = null,
   val deal: List<DealPojo>? = null)


data class CategoryPojo(
    val categoryName: String? = null,
    val categoryDesc: String? = null)

data class DealPojo(
    val dealImg: String? = null,
    val dealDesc: String? = null,
    val dealStartDate: String? = null,
    val dealEndDate: String? = null)

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

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