Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

problem to receive from firarebase and write them on recycler view

I’m trying to retrive data from firebase and write them on a custom recycler view. I wrote the code but I can’t understand why the data is not correctly taken from the database.

this is the main activity:

private lateinit var myDatabase: FirebaseDatabase
    private lateinit var myReference: DatabaseReference
    private var nameOfTournamentOnDatabase: String = "Tournament"
    private var listOfTournaments: ArrayList<TournamentData> = ArrayList()
    private var adapter: DataListAdapter? = null


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? {
        myDatabase = FirebaseDatabase.getInstance()
        myReference = myDatabase.getReference(nameOfTournamentOnDatabase)

        return inflater.inflate(R.layout.fragment_tournament_list, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var myList: RecyclerView = view.findViewById<RecyclerView>(R.id.myList)
        adapter = DataListAdapter(listOfTournaments, view.context)
        myList.layoutManager = LinearLayoutManager(view.context)
        myList.adapter = adapter
        getDataFromDatabase()
        adapter!!.notifyDataSetChanged()

    }

    private fun getDataFromDatabase(){

       myReference.addListenerForSingleValueEvent(object : ValueEventListener{
           override fun onDataChange(snapshot: DataSnapshot) {
               for(ds in snapshot.children){
                   listOfTournaments.add(TournamentData(ds.child("nameOfTournament").value.toString(),
                       ds.child("nameOfCreator").value.toString(), ds.child("numberOfCompetitors").value.toString(),
                       ds.child("nameOfCompetitors").value.toString()))
               }
           }

           override fun onCancelled(error: DatabaseError) {
               
           }

       })
    }
}

and this is the custom adapter class:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class DataListAdapter(private val dataTournamentList: ArrayList<TournamentData>, private val context: Context) : RecyclerView.Adapter<DataListAdapter.ViewHolder> (){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.card_layout, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindItem(dataTournamentList[position])
    }

    override fun getItemCount(): Int {
        return dataTournamentList.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bindItem(data: TournamentData){
            var tournamentName : TextView = itemView.findViewById(R.id.name_of_tuornament) as TextView
            var nameOfCreator : TextView = itemView.findViewById(R.id.name_of_creator) as TextView
            var numberOfCompetitors : TextView = itemView.findViewById(R.id.number_of_competitor) as TextView

            tournamentName.text = data.nameOfTournament
            nameOfCreator.text = data.nameOfCreator
            numberOfCompetitors.text = data.numberOfCompetitors
        }
    }
}

I cannot understand why the data does not arrive. The code is correctly written and has no errors and the database is set up correctly.

>Solution :

You need to call adapter.notifyDataSetChanged() after you make any changes to the list of data that the adapter shows (so inside your onDataChange).

As a general rule, I also recommend never leaving onCanceled empty as you’re ignoring possible errors. At its minimum it should be:

public void onCancelled(@NonNull DatabaseError databaseError) { 
    throw databaseError.toException(); 
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading