![](/img/trans.png)
[英]Android architecture components paging DataSource.Factory error
[英]Android Paging Library - Map Room DataSource.Factory<*, DatabaseModel> to DataSource.Factory<*, PresenterModel>
我在我的應用程序中使用了兩個模型:
Android 分頁給了我一個DataSource.Factory<*, DatabaseModel>
@Dao
interface ProjectDao {
@Query("SELECT * FROM project")
fun getAllProjects(): DataSource.Factory<Int, DatabaseModel>
...
}
當我想使用LiveData
LivePagedListBuilder(dataSourceFactory, config)
制作 LiveData 時,我需要 map:
DataSource.Factory<*, DatabaseModel>
-|----> DataSource.Factory<*, PresenterModel>
有沒有辦法實現這一點。 我也對使用 RxKotlin (RxJava) 完成的任何方法持開放態度。
這是我的做法
數據源
fun getDataSource(): DataSource.Factory<Int, DBModel> {
return database.dao.getAllData()
}
變量視圖 model
val scannedCompleteList=App.getRepository().getDataSource().toLiveData(
Config(
pageSize = 60,
enablePlaceholders = true,
maxSize = 200
)
)
現在我有一個綁定適配器,我將數據從 db model 轉換為域 model
@BindingAdapter("setData")
fun setImageScanned(recyclerView: RecyclerView, data: List<DBModel>?) {
val adapter = recyclerView.adapter as MyAdapter
adapter.submitList(it.asDomainModel())
}
}
因此,您正在觀察片段中的數據,因此您可以將數據轉換為觀察者內部的演示者。 asDomainModel 是一個擴展 function 進行轉換
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.