Search code examples
androidkotlinfirebase-realtime-databaseandroid-recyclerviewdata-class

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

The function below is in the kotlin fragment class for the xml layout containing the recycler view

 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 is the xml containing the recycler view tag

  • R.layout.users is the data xml

  • view.recyclerView is the recycler view tag

  • BalAdapter is the adapter class, can be provided on request, but I feel the code there is correct

  • UserInfo.Uids is the data class, contains name and email

  • adapter variable was declared before the override fun onCreateView


Solution

  • 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)
    

    You'll need to add the values you read from firebase into an arraylist, then pass that arraylist into the adapter

    For example:

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