Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 分页库recyclerview不显示更新的数据(DB+网络)_Android_Kotlin_Android Recyclerview_Android Paging Library - Fatal编程技术网

Android 分页库recyclerview不显示更新的数据(DB+网络)

Android 分页库recyclerview不显示更新的数据(DB+网络),android,kotlin,android-recyclerview,android-paging-library,Android,Kotlin,Android Recyclerview,Android Paging Library,我有一个使用分页库显示的人员列表,从本地数据库加载初始数据,一旦需要更多数据,我调用API从服务器获取数据并将其保存到数据库中 我的片段: Class PersonListFragment: Fragment(){ override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) recycler_v

我有一个使用分页库显示的人员列表,从本地数据库加载初始数据,一旦需要更多数据,我调用API从服务器获取数据并将其保存到数据库中

我的片段:

Class PersonListFragment: Fragment(){

   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)
      recycler_view.layoutManager = LinearLayoutManager(context)
      recycler_view.adapter = personListAdapter

      personListViewModel.getPersonsLiveData().observe(viewLifecycleOwner, Observer {
         personListAdapter.submitList(it)
      })
   }
}
我的ViewModel:

class PersonViewModel: ViewModel{

   lateinit var personsLiveData: LiveData<PagedList<Person>>
   lateinit var boundaryCallback: TransactionBoundaryCallback

   fun getPersonsLiveData(): LiveData<PagedList<Person>> = personsLiveData

   Init {
      val dataSourceFactory = getPersons()
      val config = PagedList.Config.Builder()
            .setEnablePlaceholders(false)
            .setPageSize(10)
            .build()

      boundaryCallback = PersonBoundaryCallback()

      personsLiveData = LivePagedListBuilder(dataSourceFactory, config)
            .setBoundaryCallback(boundaryCallback)
            .build()
   }

   private fun getPersons(): DataSource.Factory<Int, Persons> = personDAO.getAllPersons()

}
列表适配器:

class PersonListAdapter: PagedListAdapter<Person, PersonListAdapter.PersonViewHolder>(DIFF_CALLBACK) {
   ...

   companion object {

      val DIFF_CALLBACK: DiffUtil.ItemCallback<Person> = object : DiffUtil.ItemCallback<Person>() {

         override fun areItemsTheSame(oldItem: Person, newItem: Person): Boolean {
            return oldItem.id == newItem.id
         }

         override fun areContentsTheSame(oldItem: Person, newItem: Person): Boolean {
             return oldItem == newItem
         }
      }
   }
}
道:

回调类:

class PersonBoundaryCallback: PagedList.BoundaryCallback<Person>(){

   override fun onZeroItemsLoaded() {
      requestAndSave()
      super.onZeroItemsLoaded()
   }

   override fun onItemAtEndLoaded(itemAtEnd: Person) {
        ...
      //request data from API and saves it into DB
      requestAndSave()
      super.onItemAtEndLoaded(itemAtEnd)

   }
}
在一切正常之前,获取的数据将保存到数据库中,但recyclerview不会用新数据更新列表,而是只显示初始数据


我已经搜索过这个话题,并对它进行了调整,但到目前为止还没有找到为什么不适合我。

很抱歉回答得太晚,但我很忙。我发现Android页面上的答案非常明确,不推荐使用:

PagedList/DataSource对是数据集的快照。一个新的 如果发生更新,则必须创建一对PagedList/DataSource, 例如,发生重新排序、插入、删除或内容更新。A. 数据源必须检测到它无法继续加载其快照 例如,当数据库查询注意到一个表无效时, 并调用invalidate。然后将创建一个新的PagedList/DataSource对 创建以从数据库查询的新状态加载数据


因此,一旦通过向下滑动列表调用刷新,我的数据就会得到更新。

u可能需要参考本教程的示例实现,请记住替换android.arch.paging。。。不推荐使用androidx。分页。。。包裹式喷气背包。希望这有帮助!谢谢,我会查一下的
class PersonBoundaryCallback: PagedList.BoundaryCallback<Person>(){

   override fun onZeroItemsLoaded() {
      requestAndSave()
      super.onZeroItemsLoaded()
   }

   override fun onItemAtEndLoaded(itemAtEnd: Person) {
        ...
      //request data from API and saves it into DB
      requestAndSave()
      super.onItemAtEndLoaded(itemAtEnd)

   }
}