android
android

Reputation: 135

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 RecyclerView, my data is going to Firebasebase but not showing in the RecyclerView and I get no errors.

I couldn't find any tutorials for my problem with Kotlin language

Here is my Adapter:

class QuestionAdapter(var items : ArrayList<Question>): RecyclerView.Adapter<QuestionAdapter.ViewHolder>() {

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

override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
    var userDto = items[position]
    holder?.titleTextView?.text = userDto.titlu
    holder?.msgTextView?.text = userDto.uid
}

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
    val itemView = LayoutInflater.from(parent?.context)
            .inflate(R.layout.conv_layout, parent, false)

    return ViewHolder(itemView)
}

class ViewHolder(row: View) : RecyclerView.ViewHolder(row) {
    var titleTextView: TextView? = null
    var msgTextView: TextView? = null

    init {
        this.titleTextView = row?.findViewById<TextView>(R.id.titleTextView)
        this.msgTextView = row?.findViewById<TextView>(R.id.msgTextView)
    }
}
}

And here is my code for Activity:

 private fun populalteQuestionsList() {
    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 {
        override fun onDataChange(dataSnapshot: DataSnapshot?) {
            mRecyclerView.layoutManager = LinearLayoutManager(this@MainActivity)
            mRecyclerView.setHasFixedSize(true)
            val  questions = ArrayList<Question>()
            val adapter = QuestionAdapter(questions)
            mRecyclerView.adapter = adapter
        }
        override fun onCancelled(error: DatabaseError?) {
        }
    })
}

Upvotes: 2

Views: 1397

Answers (2)

user4571931
user4571931

Reputation:

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

private fun populalteQuestionsList() {
    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 {
        override fun onDataChange(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()        
        }
        override fun onCancelled(error: DatabaseError?) {
        }
    })
}

and i hope in adapter have below method ...

@Override
public int getItemCount() {
    return items.size();
}

Upvotes: 1

AskNilesh
AskNilesh

Reputation: 69671

Recyclerview is not showing data from Firebase Kotlin

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

check in your code

Try this

override fun onDataChange(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
        }

Upvotes: 3

Related Questions