问题描述
我正在创建具有MVVM架构的应用程序,但遇到了一个问题,该问题是获取要在View中显示的LiveData列表.
I am creating an app with MVVM architecture and I ran into an issue of getting a list of LiveData to show in the View.
在我的ViewModel中,我有一个getAll()函数,该函数使用Room从数据库中检索字符串列表.从那里,我得到了字符串并调用我的Retrofit函数,将每个字符串分别发送到返回对象的Web服务器.这是我的问题所在.
In my ViewModel I have a getAll() function that retrieves a list of strings from the database using Room. From there, I get the strings and call my Retrofit function to send each string individually to a web-server that returns an object. Here is where my issue occurs.
从我在网上看到的MVVM教程中,它们通常具有LiveData>样式,但是在这种情况下,因为我分别获取每个对象,所以它变成了List>,但是我认为这不是正确的做法,因为在我看来视图我需要做一个ForEach循环来观察列表中的每个LiveData对象.
From the MVVM tutorials I see online, they usually have the LiveData> style but in this since I am getting each object individually, it becomes List> but I don't think this is the correct way of doing it because in my View I would need to do a ForEach loop to observe each LiveData object in the list.
我尝试了其他解决方法,但似乎没有用.有更好的方法吗?
I have tried other work arounds but it doesn't seem to work. Is there a better way of doing this?
DAO
@Query("SELECT * FROM table")
fun getAll(): LiveData<List<String>>
存储库
fun getAll(): LiveData<List<String>> {
return dao.getAll()
}
fun getRetrofitObject(s: String): LiveData<RetrofitObject> {
api = jsonApi.getRetrofitObjectInfo(s, API_KEY)
val retrofitObject: MutableLiveData<RetrofitObject> = MutableLiveData()
api.enqueue(object : Callback<RetrofitObject> {
override fun onFailure(call: Call<RetrofitObject>?, t: Throwable?) {
Log.d("TEST", "Code: " + t.toString())
}
override fun onResponse(call: Call<RetrofitObject>?, response: Response<RetrofitObject>?) {
if (response!!.isSuccessful) {
retrofitObject.value = response.body()
}
}
})
return retrofitObject
}
MainActivityViewModel(ViewModel)
MainActivityViewModel (ViewModel)
var objectList ArrayList<LiveData<retrofitObject>> = ArrayList()
// This is getting objects using Retrofit
fun getRetrofitObject(s: String): LiveData<retrofitObject> {
return repo.getRetrofitObject(s)
}
// This is getting all the strings from the internal database
fun getAll(): ArrayList<LiveData<retroFitObject>> {
repo.getAll().value?.forEach {it ->
objectList.add(getRetrofitObject(it)) //How else would I be able to do this?
}
return objectList
}
MainActivity(视图)
MainActivity (View)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mainActivityViewModel.getAll().forEach {
it.observe(this, Observer {it ->
mainActivityViewModel.objectList.add(it) //Here is part of the issue since I don't want to use a forloop in the View
})
}
adapter.objectList = mainActivityViewModel.objectList
recyclerView.adapter = adapter
}
谢谢,让我知道是否还有其他需要或困惑!
Thanks, let me know if there is anything else needed or confusion!
推荐答案
可能存在其他我不知道的方法.
There might be other methods exist I don't know about them.
但是您可以使用Transformations处理情况,以获取更多信息,请查看文档页面. Transformations.switchMap(LiveData触发器,功能> func)
But you can deal with situation using Transformations for more information please look at documentation page.Transformations.switchMap(LiveData trigger, Function> func)
您不必将实时数据观察器放入for循环中.
You don't have to put the live data observer inside for loop.
这篇关于Android LiveData:如何执行ArrayList< LiveData< RetrofitObject>> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!