Home > Net >  problem to receive from firarebase and write them on recycler view
problem to receive from firarebase and write them on recycler view

Time:02-25

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:

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.

CodePudding user response:

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(); 
}
  • Related