实时数据库中未显示回收站视图

时间:2021-03-18 18:04:08

标签: android kotlin firebase-realtime-database android-recyclerview data-class

我有一个简单的回收器视图,它从数据库中读取一个列表,当我将它们记录到控制台时我可以看到数据,但它没有显示在回收器视图中

下面的函数在 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.
            }




        })
        adapter = BalAdapter(requireContext(),  ArrayList<UserInfo.Uids>(),  R.layout.users)
        view.recyclerView.adapter = adapter
        adapter?.notifyDataSetChanged()

        return  view

    }
<块引用>

R.layout.fragment_chat_list 是包含回收者视图标记的 xml

<块引用>

R.layout.users 是数据xml

<块引用>

view.recyclerView 是回收者视图标签

<块引用>

BalAdapter 是适配器类,可以根据要求提供,但我觉得那里的代码是正确的

<块引用>

UserInfo.Uids 是数据类,包含 nameemail

<块引用>

adapter 变量在 override fun onCreateView

之前声明

0 个答案:

没有答案
相关问题