有一个可变的LiveData Holding 2数组“ deal”和“ category”,我需要在不同的适配器中对此进行解析。
有没有办法将1个可变的实时数据转换为2个数组,然后将它们解析为两个不同的适配器
假设存在MutableVariable名称se
private lateinit var mHomePojo: MutableLiveData<List<HomePojo>>
解析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"
}
]
}
有什么方法可以将private lateinit var mHomePojo: MutableLiveData<List<HomePojo>>
解析为lateinit var mDealModel: MutableLiveData<List<DealModel>>
并
lateinit var mCategoryModel: MutableLiveData<List<CategoryModel>>
我是MVVM的新手,请帮助
答案 0 :(得分:0)
我认为Transformations可以帮助您将家庭实时数据分离为具有指定属性的两个单独的livedata对象。下面是这段代码。 (注意:例如,未使用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)