在recyclerview kotlin中插入Firebase快照 [英] inserting firebase snapshot in recyclerview kotlin
问题描述
我有一个简单的回收器视图,该视图从数据库中读取一个列表,将数据记录到控制台时可以看到数据,但它没有显示在回收器视图中
I have a simple recycler view that reads a list from the database, I can see data when I log them to the console, but it doesn't show in the recycler view
下面的功能在包含回收者视图的xml布局的kotlin片段类中
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
是包含回收者视图标记的xmlR.layout.fragment_chat_list
is the xml containing the recycler view tagR.layout.users
是数据xmlview.recyclerView
是回收站视图标签BalAdapter
是适配器类,可以应要求提供,但是我觉得那里的代码是正确的BalAdapter
is the adapter class, can be provided on request, but I feel the code there is correctUserInfo.Uids
是数据类,包含name
和email
adapter
变量在覆盖onCreateView
推荐答案
看起来好像您没有在recyclerview中填充任何数据.适配器始终使用空的
ArrayList
:Doesn't look like you're filling the recyclerview up with any data. The adapter is always initialized with an empty
ArrayList
:/* This is an empty ArrayList */ adapter = BalAdapter(requireContext(), ArrayList<UserInfo.Uids>(), R.layout.users)
您需要将您从firebase读取的值添加到arraylist中,然后将该arraylist传递到适配器中
You'll need to add the values you read from firebase into an arraylist, then pass that arraylist into the adapter
例如:
//Before any firebase requests: val balList = ArrayList<UserInfo.Uids>() adapter = BalAdapter(requireContext(), balList, R.layout.users) ... //After you get data from firebase val bal = snapshot.getValue<UserInfo.Uids>() balList.add(bal) adapter.notifyDataSetChanged()
这篇关于在recyclerview kotlin中插入Firebase快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!