Home > Software design >  how to display each array item to recyclerview in kotin (FireStore)
how to display each array item to recyclerview in kotin (FireStore)

Time:09-12

Database

enter image description here

output

enter image description here

it only showing the last entry in array i wann show each entry

Adapter code

package com.example.caterers_app



class CommentAdapter(private val commentlist : ArrayList<CommentDataClass>) : RecyclerView.Adapter<CommentAdapter.MyViewHolder>() {


override fun onCreateViewHolder(
    parent: ViewGroup,
    viewType: Int
): CommentAdapter.MyViewHolder {
    val itemView = LayoutInflater.from(parent.context).inflate(R.layout.commentstemplate,
        parent,false)

    return MyViewHolder(itemView)    }



override fun getItemCount(): Int {
    Log.d("size", commentlist.size.toString())

    return commentlist.size
}

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

    val comment : TextView = itemView.findViewById(R.id.comment)

}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    val comments = commentlist[position]
    comments.list?.forEach {
        holder.comment.text = it.toString()
    }
    //holder.comment.text = comments.list.toString()
}

}

if i use holder.comment.text = comments.list.toString() it will display all array items in single recyclerview block

data class

package com.example.caterers_app

data class CommentDataClass(val list : List<String>? = null)
{

}

fragment

package com.example.caterers_app




class Comments(val doc : String) : Fragment() {


    private lateinit var docid : String
    private lateinit var recyclerView: RecyclerView
    private lateinit var commentarraylist : ArrayList<CommentDataClass>
    private lateinit var adapter: CommentAdapter
    private lateinit var db : FirebaseFirestore
init {
    docid = doc
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view =  inflater.inflate(R.layout.fragment_comments, container, false)

    recyclerView = view.findViewById(R.id.comentoo)
    recyclerView.layoutManager = LinearLayoutManager(context)
    recyclerView.setHasFixedSize(true)

    commentarraylist = arrayListOf()
    adapter = CommentAdapter(commentarraylist)
    recyclerView.adapter = adapter


    EventChangeListner()

    return view
}

companion object {

    fun newInstance(): Comments {
        return Comments("null")
    }
}


private fun EventChangeListner() {
    var obj = CommentDataClass()

    db = FirebaseFirestore.getInstance()
    db.collection("User_Events")
        .addSnapshotListener(object : EventListener<QuerySnapshot> {
            override fun onEvent(value: QuerySnapshot?, error: FirebaseFirestoreException?) {

                if(error!= null) {
                    return
                }

                for (dc : DocumentChange in value?.documentChanges!!){

                    if(dc.type == DocumentChange.Type.ADDED){
                        //eventarraylist.add(dc.document.toObject(EventsData::class.java))
                        if(dc.document.id.toString() == docid.toString()){
                            obj = dc.document.toObject(CommentDataClass::class.java)
                            commentarraylist.add(obj)
                        }

                    }
                }

                adapter.notifyDataSetChanged()

            }
        })


}

}

i want to display each array item in seperate recyclerview item how can i do that in kotlin using firebase firestore

CodePudding user response:

You're passing an ArrayList<CommentDataClass> into the adapter when you likely want to pass List<String> instead, since CommentDataClass contains that List<String>?

You'll probably want to make CommentAdapter.commentlist modifiable:

class CommentAdapter() : RecyclerView.Adapter<CommentAdapter.MyViewHolder>() {
  var commentlist = emptyList<String>()
  ...
}

and overwrite it once you have your CommentDataClass:

if(dc.document.id.toString() == docid.toString()){
  obj = dc.document.toObject(CommentDataClass::class.java)
  adapter.commentlist = obj.list
}
  • Related