0

I need to display data from firebase database using a recyclerView. everything works fine, I can see the cards and all but there wont be and data on the card.

here is my adapter class:

class MyAdapter(c: Context, t: ArrayList<Thoughts>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

var context : Context
    var theThoughts : ArrayList<Thoughts>

    init{
        context = c
        theThoughts = t
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(LayoutInflater.from(context).inflate(R.layout.card_view01, parent, false))
    }//end onCreateViewHolder

    override fun getItemCount(): Int {
        return theThoughts.size
    }//end getItemCount

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        //holder.theTopicAdapter.text[position]
        //holder.theThoughtAdapter.text[position]
        //it works fine withOUT these two lines up
    }//end onBindViewHolder

    class MyViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {

        var theTopicAdapter: TextView
        var theThoughtAdapter: TextView

        init{
            //super.itemView
            theTopicAdapter = itemView.textViewCard01
            theThoughtAdapter = itemView.textViewCard02
        }

        fun MyViewHolder(itemView: View){
            super.itemView
            theTopicAdapter = itemView.textViewCard01
            theThoughtAdapter = itemView.textViewCard02
        }//end MyViewHolder function
        }//end MyViewHolder class
}//end MyAdapter class

this is the code where I get the data and display it:

databaseReference.addValueEventListener(object: ValueEventListener {
            override fun onDataChange(p0: DataSnapshot) {
                if (p0.exists()){
                    for (thought in p0.children){
                        //var theTopicGetter = thought.child("theTopic").value.toString()
                        val t: Thoughts = thought.getValue(Thoughts::class.java)!!
                        list.add(t)
                    }//end for loop
                }//end if
                adapter = MyAdapter(this@SeePreviousThoughts, list)
                recyclerView.adapter = adapter
            }//end onDataChange
            override fun onCancelled(p0: DatabaseError) {
                Toast.makeText(this@SeePreviousThoughts, "error, please try again.", Toast.LENGTH_SHORT).show()
            }
        })//end the listener

this is the class thoughs:

class Thoughts() {
    lateinit var theID: String
    lateinit var theCode: String
    lateinit var theTopic: String
    lateinit var theThought: String

    constructor(theID: String, theCode: String, theTopic: String, theThought: String) : this() {
            this.theID = theID
            this.theCode = theCode
            this.theTopic = theTopic
            this.theThought = theThought
    }

}

I should get the data from the database and it should be displayed on each card, but the cards are simply empty. this is a screenshot of the empty recyclerView the words topic and thoughts are default from the xml file.

I guess that my problem is that the function MyViewHolder in class MyViewHolder is never used... a screenshot of the code that isn't being used

shohoud n
  • 3
  • 2
  • Fill list elements in `onBindViewHolder(holder: MyViewHolder, position: Int)` with data from `theThougts`. Rest of code seems to be working. – Karol Kulbaka Apr 23 '19 at 06:26
  • @KarolKulbaka when I uncomment the lines in my `onBindViewHolder` the app crashes. I'll edit my question with something that I just found. can you please check it out. thank you so much. – shohoud n Apr 23 '19 at 09:43
  • first of all, proper view fill will look like this: `holder.theTopicAdapter.text = theThoughts[position].someTextData` – Karol Kulbaka Apr 23 '19 at 10:10
  • Holder constructor is used as `MyViewHolder(LayoutInflater.from(context).inflate(R.layout.card_view01, parent, false))`. Are there some differences between your current adapter implementation and code in question? – Karol Kulbaka Apr 23 '19 at 10:14
  • @KarolKulbaka Thank you, in the `onBindViewHolder` I did what you said and I wrote this code: `holder.theTopicAdapter.text = theThoughts[position].toString()` it works better but now the text I get in the card says my project ID in firebase and some numbers. should I post a picture of that? BTW, my class "Thoughts" in firebase has 4 elements, but I only wanna display 2 items only. does that effect the outcome? – shohoud n Apr 23 '19 at 11:49
  • There's no need to send pictures. `theThoughts[position].toString()` is not proper. Add `Thoughts` implementation to the question and I will show you what exactly is need to be done. – Karol Kulbaka Apr 23 '19 at 12:04
  • @KarolKulbaka sorry, you mean that you need the class Thoughts? – shohoud n Apr 23 '19 at 12:05
  • Yes, in class Thoughts there should be a what is needed – Karol Kulbaka Apr 23 '19 at 12:15
  • @KarolKulbaka I added it – shohoud n Apr 23 '19 at 12:17
  • `holder.theTopicAdapter.text = theThoughts[position].theTopic` `holder.theThoughtAdapter.text = theThoughts[position].theThought` This should work as you want – Karol Kulbaka Apr 23 '19 at 12:24
  • Thank you so much that did what I want. – shohoud n Apr 23 '19 at 12:28
  • Should I add the answer or mayby there is something else? – Karol Kulbaka Apr 23 '19 at 12:29
  • @KarolKulbaka no. please add your answers. it solved everything perfectly! – shohoud n Apr 23 '19 at 12:40

1 Answers1

0

Problem was in override fun onBindViewHolder(holder: MyViewHolder, position: Int) method. This adapter works:

class MyAdapter(val context: Context, val  theThoughts: ArrayList<Thoughts>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(LayoutInflater.from(context).inflate(R.layout.card_view01, parent, false))
    }//end onCreateViewHolder

    override fun getItemCount(): Int {
        return theThoughts.size
    }//end getItemCount

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.theTopicAdapter.text = theThoughts[position].theTopic
        holder.theThoughtAdapter.text = theThoughts[position].theThought
    }//end onBindViewHolder

    class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {

        lateinit var theTopicAdapter: TextView
        lateinit var theThoughtAdapter: TextView

        fun MyViewHolder(itemView: View){
            super.itemView
            theTopicAdapter = itemView.textViewCard01
            theThoughtAdapter = itemView.textViewCard02
        }//end MyViewHolder function
    }//end MyViewHolder class
}//end MyAdapter class 
Karol Kulbaka
  • 1,136
  • 11
  • 21