2

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?) {
        }
    })
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
android
  • 135
  • 1
  • 11

2 Answers2

3

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
        }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

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();
}
  • when you copy getItemCount() method into adapter it automatic convert into kotlin. and also check your firebase database store question class data and check onDataChange method give data or not. –  Mar 21 '18 at 11:38
  • 1
    I know but this line, I can't convert and it's not working "questions.addAll(dataSnapshot.getValue(Question.class));" – android Mar 21 '18 at 11:39
  • sorry for that you can add this line val question = dataSnapshot.getValue(Question::class.java) questions.add(question) –  Mar 21 '18 at 11:46
  • sorry that i'm bothering you, val question = dataSnapshot.getValue(Question::class.java) is instead of val questions = ArrayList() ? – android Mar 21 '18 at 11:48
  • i don't know still nothing. would mind if i would start a discussion chat with you ? – android Mar 21 '18 at 11:51
  • you can check onDataChange Method call or not.then check you firebase data base node is given question data or not. –  Mar 21 '18 at 11:55