如何解决ListAdapter 未使用来自 livedata
我使用带 MVVM 的 firebase firestore 下载数据,我使用存储库层在应用程序级别获取和保存数据,使用 viewModel 层观察更改并调用存储库,以及显示数据的片段.问题是,即使我可以成功获取数据,它也没有显示在 recyclerview 上。
存储库类:
fun fetchAppsFromFirestore() {
val list = mutableListOf<AppModel>()
firestore.collection(APPS_COLLECTION).get().addOnSuccessListener { querySnapshot ->
for (doc in querySnapshot) {
val app = doc.toObject(AppModel::class.java).also {
it.docId = doc.id
}
list.add(app)
}
//This is a global mutableLiveData
readingLiveData.value = list
}
}
ViewModel 类:
fun getData() : LiveData<AppModelReadingResponse> {
repo.fetchAppsFromFirestore()
return repo.readingLiveData
}
到目前为止,代码做了它应该做的事情,如果我登录 viewModel,列表中的所有项目都在那里。
适配器类:
class FeaturedAppAdapter(val context: Context) :
ListAdapter<AppModel,FeaturedAppAdapter.FeaturedAppViewHolder>(AppModelComparator()) {
class AppModelComparator : DiffUtil.ItemCallback<AppModel>() {
override fun areItemsTheSame(oldItem: AppModel,newItem: AppModel): Boolean {
return oldItem.docId == newItem.docId
}
override fun areContentsTheSame(oldItem: AppModel,newItem: AppModel): Boolean {
return oldItem == newItem
}
}
inner class FeaturedAppViewHolder(private val binder: RowFeaturedAppsBinding) :
RecyclerView.ViewHolder(binder.root) {
fun bind(app: AppModel) {
binder.rowFeaturedAppsTxtAppName.text = app.appName
binder.rowFeaturedAppsTxtAppDescription.text = app.appDesc
}
}
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): FeaturedAppViewHolder {
return FeaturedAppViewHolder(
RowFeaturedAppsBinding.inflate(
LayoutInflater.from(parent.context),parent,false
)
)
}
override fun onBindViewHolder(holder: FeaturedAppViewHolder,position: Int) {
val app = getItem(position)
holder.bind(app)
}
}
在我检索数据的片段上:
class HomeFragment : Fragment() {
companion object {
private const val TAG = "tag_log_info"
}
//Layout components
private val binder by lazy {
FragmentHomeBinding.inflate(layoutInflater)
}
//ViewModel
private val appsViewModel: AppsViewModel by viewModels {
AppsViewModelFactory((requireActivity().application as MyApplication).appsRepository)
}
//RecyclerView
private lateinit var mAdapter: FeaturedAppAdapter
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View {
setupRecyclerView()
return binder.root
}
private fun setupRecyclerView() {
mAdapter = FeaturedAppAdapter(requireContext())
with(binder.fragmentHomeRecyclerView){
this.adapter = mAdapter
this.layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.VERTICAL,false)
this.setHasFixedSize(true)
}
observeData()
}
private fun observeData() {
appsViewModel.getData().observe(viewLifecycleOwner) {
Log.i(TAG,"observeData: $it") // This log shows that the data is here
mAdapter.submitList(it)
}
}
}
所以问题是,即使日志显示我有片段上的数据列表,当我调用 submitList(it)
时,没有任何反应,我无法弄清楚原因。我在这里做错了什么?
解决方法
在将数据设置为适配器后,在observeData 函数中,您必须调用notifyDataSetChanged
函数以让适配器知道数据发生了变化。
所以你的观察者函数变成了
private fun observeData() {
appsViewModel.getData().observe(viewLifecycleOwner) {
Log.i(TAG,"observeData: ${it.list}") // This log shows that the data is here
mAdapter.submitList(it.list)
mAdapter.notifyDataSetChanged()
}
}
如果这不起作用,请告诉我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。