Skip to content Skip to sidebar Skip to footer

Recyclerview Is Not Showing Data From Firebase Kotlin

As title says, I'm using since yesterday Kotlin. I want to populate a RecyclerView with data from Firebase. I tried something, I built an Adapter and tried to populate the Recycler

Solution 1:

Recyclerview is not showing data from Firebase Kotlin

Because you are not adding any data inside your questionsArrayList<Question>()

check in your code

Try this

overridefunonDataChange(dataSnapshot: DataSnapshot?) {
            mRecyclerView.layoutManager = LinearLayoutManager(this@MainActivity)
            mRecyclerView.setHasFixedSize(true)
            val  questions = ArrayList<Question>()
            val adapter = QuestionAdapter(questions)
           // add data here inside your questions ArrayList<Question>()
            mRecyclerView.adapter = adapter
        }

Solution 2:

i hope in your adater define getItemCount() method then you can change below code ...

privatefunpopulalteQuestionsList() {
    val mChatDatabaseReference = FirebaseDatabase.getInstance().reference.child(Constants.USERS).child(mUserID).child(Constants.CHAT).child(Constants.QUESTION)
    val query = mChatDatabaseReference.orderByChild("time")
    query.addListenerForSingleValueEvent(object: ValueEventListener {
        overridefunonDataChange(dataSnapshot: DataSnapshot?) {
            mRecyclerView.layoutManager = LinearLayoutManager(this@MainActivity)
            mRecyclerView.setHasFixedSize(true)
            val  questions = ArrayList<Question>()
            val question = dataSnapshot.getValue(Question::class.java)
            questions.add(question)
            val adapter = QuestionAdapter(questions)
            mRecyclerView.adapter = adapter
            adapter.notifyDataSetChanged()        
        }
        overridefunonCancelled(error: DatabaseError?) {
        }
    })
}

and i hope in adapter have below method ...

@Override
publicintgetItemCount() {
    return items.size();
}

Post a Comment for "Recyclerview Is Not Showing Data From Firebase Kotlin"