0

enter image description here

I AM trying to create other constructor in recyclerView adapter but getting error please tell me how to create secondary constructor in recycler adapter taking arraylist parameter in kotlin android studio

class CustomRecyclerAdapter constructor(
    val context1: Context,
    val topics: Array<String>,
    private var alist: ArrayList<Array<String>>
) : RecyclerView.Adapter<CustomRecyclerAdapter.ViewHolder>() {

    constructor(sss: ArrayList<Array<String>>, ttt: Array<String>) : this(sss) {
        this.alist = sss
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): CustomRecyclerAdapter.ViewHolder {
        val layout = LayoutInflater.from(parent.context).inflate(R.layout.cardlayout, parent, false)
        return ViewHolder(layout)

    }

    override fun onBindViewHolder(holder: CustomRecyclerAdapter.ViewHolder, position: Int) {
        holder.topicTextView.text = topics[position]
    }

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

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var topicTextView: TextView = itemView.findViewById(R.id.gir_topics_tvid)


    }

}
Sergio
  • 27,326
  • 8
  • 128
  • 149

1 Answers1

1

Please use these constructors:

class CustomRecyclerAdapter constructor(
    val context: Context, 
    val topics: Array<String> = emptyArray(),
    private var alist: ArrayList<String> 
 ) : RecyclerView.Adapter<CustomRecyclerAdapter.ViewHolder>() {

    constructor(context: Context, sss: ArrayList<String>) : this(context, alist = sss) 

    // ...
}

In the primary constructor you need to specify a default value for topics. Also add context: Context parameter to the secondary constructor and pass it to this() when calling the primary constructor.

Sergio
  • 27,326
  • 8
  • 128
  • 149