【发布时间】:2021-06-16 08:36:17
【问题描述】:
我有一个简单的回收器视图,它从数据库中读取一个列表,当我将它们记录到控制台时我可以看到数据,但它没有显示在回收器视图中
下面的函数在 kotlin 片段类中,用于包含回收器视图的 xml 布局
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
// Inflate the layout for this fragment
val view: View = inflater.inflate(R.layout.fragment_chat_list, container, false)
view.recyclerView?.setHasFixedSize(true)
view.recyclerView?.layoutManager = LinearLayoutManager(context)
ref?.addChildEventListener(object : ChildEventListener {
override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) {
val bal = snapshot.getValue<UserInfo.Uids>()
if (bal != null) {
UserInfo.Uids(bal.email,bal.uid,bal.displayName)
}
Log.d("RecyclerView", UserInfo.Uids().toString())
Log.d("RecyclerView", bal.toString())
adapter = BalAdapter(requireContext(), ArrayList<UserInfo.Uids>(), R.layout.users)
adapter!!.notifyDataSetChanged()
}
override fun onChildChanged(snapshot: DataSnapshot, previousChildName: String?) {
TODO("Not yet implemented")
}
override fun onChildRemoved(snapshot: DataSnapshot) {
TODO("Not yet implemented")
}
override fun onChildMoved(snapshot: DataSnapshot, previousChildName: String?) {
TODO("Not yet implemented")
}
override fun onCancelled(p0: DatabaseError) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
})
return view
}
-
R.layout.fragment_chat_list是包含recycler view标签的xml -
R.layout.users是数据xml -
view.recyclerView是回收站视图标签 -
BalAdapter是适配器类,可按要求提供,但我觉得那里的代码是正确的 -
UserInfo.Uids为数据类,包含name和email -
adapter变量在override fun onCreateView之前声明
【问题讨论】:
标签: android kotlin firebase-realtime-database android-recyclerview data-class