【问题标题】:inserting firebase snapshot in recyclerview kotlin在recyclerview kotlin中插入firebase快照
【发布时间】: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为数据类,包含nameemail

  • adapter 变量在 override fun onCreateView 之前声明

【问题讨论】:

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


    【解决方案1】:

    看起来您没有在 recyclerview 中填充任何数据。适配器总是用空的ArrayList初始化:

                                        /* This is an empty ArrayList */
    adapter = BalAdapter(requireContext(),  ArrayList<UserInfo.Uids>(),  R.layout.users)
    

    您需要将从 firebase 读取的值添加到数组列表中,然后将该数组列表传递给适配器

    例如:

    //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()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多