将对象添加到可变的实时数据列表
Adding an object to a mutable live data list
我正在尝试将两家商店添加到可变列表中,如下所示:
private var _shopList = MutableLiveData<List<Shop>>()
var shopList: LiveData<List<Shop>> = ()
get() = _shopList
// This function will get the arrayList items
fun getArrayList(): MutableLiveData<List<Shop>>
{
// Here we need to get the data
val shop1 = Shop("AOB", "Lat and Long","LA")
val shop2 = Shop("Peach", "Lat and Long","Vegas")
_shopList.add(shop1)
_shopList.add(shop2)
return _shopList
}
但是它说没有引用添加函数?
来自文档:
List:元素的通用有序集合。此接口中的方法仅支持 read-only 访问列表; read/write 通过 MutableList 接口支持访问。
MutableList: 支持添加和删除元素的通用有序元素集合。
您可以修改 MutableList:更改、删除、添加...它的元素。在列表中你只能阅读它们。
解决方案-
private var _shopList = MutableLiveData<List<Shop>>() // List is fine here as read only
val shopList: LiveData<List<Shop>> // List is fine here as read only
get() = _shopList
// This function will get the arrayList items
fun getArrayList(): MutableLiveData<List<Shop>> {
// Here we need to get the data
val shop1 = Shop("AOB", "Lat and Long", "LA") // var to val
val shop2 = Shop("Peach", "Lat and Long", "Vegas") //var to val
_shopList.value = mutableListOf(shop1, shop2) // assigns a mutable list as value to the live data which can be observed in the view
return _shopList
}
解决方法如下:
private var _shopList = MutableLiveData<MutableList<Shop>>()
val shopList: LiveData<MutableList<Shop>>
get() = _shopList
// This function will get the arrayList items
fun getArrayList(): MutableLiveData<MutableList<Shop>>
{
// Here we need to get the data
var shop1 = Shop("AOB", "Lat and Long","LA")
var shop2 = Shop("Peach", "Lat and Long","Vegas")
_shopList.value = mutableListOf(shop1,shop2)
return _shopList
}
我正在尝试将两家商店添加到可变列表中,如下所示:
private var _shopList = MutableLiveData<List<Shop>>()
var shopList: LiveData<List<Shop>> = ()
get() = _shopList
// This function will get the arrayList items
fun getArrayList(): MutableLiveData<List<Shop>>
{
// Here we need to get the data
val shop1 = Shop("AOB", "Lat and Long","LA")
val shop2 = Shop("Peach", "Lat and Long","Vegas")
_shopList.add(shop1)
_shopList.add(shop2)
return _shopList
}
但是它说没有引用添加函数?
来自文档:
List:元素的通用有序集合。此接口中的方法仅支持 read-only 访问列表; read/write 通过 MutableList 接口支持访问。
MutableList: 支持添加和删除元素的通用有序元素集合。
您可以修改 MutableList:更改、删除、添加...它的元素。在列表中你只能阅读它们。
解决方案-
private var _shopList = MutableLiveData<List<Shop>>() // List is fine here as read only
val shopList: LiveData<List<Shop>> // List is fine here as read only
get() = _shopList
// This function will get the arrayList items
fun getArrayList(): MutableLiveData<List<Shop>> {
// Here we need to get the data
val shop1 = Shop("AOB", "Lat and Long", "LA") // var to val
val shop2 = Shop("Peach", "Lat and Long", "Vegas") //var to val
_shopList.value = mutableListOf(shop1, shop2) // assigns a mutable list as value to the live data which can be observed in the view
return _shopList
}
解决方法如下:
private var _shopList = MutableLiveData<MutableList<Shop>>()
val shopList: LiveData<MutableList<Shop>>
get() = _shopList
// This function will get the arrayList items
fun getArrayList(): MutableLiveData<MutableList<Shop>>
{
// Here we need to get the data
var shop1 = Shop("AOB", "Lat and Long","LA")
var shop2 = Shop("Peach", "Lat and Long","Vegas")
_shopList.value = mutableListOf(shop1,shop2)
return _shopList
}