如何在 kotlin android studio 中采用 arraylist 参数在回收器适配器中创建辅助构造函数

how to create secondary constructor in recycler adapter taking arraylist parameter in kotlin android studio

enter image description here

我正在尝试在 recyclerView 适配器中创建其他构造函数但出现错误 请告诉我如何在回收器适配器中创建辅助构造函数,在 kotlin android studio

中采用 arraylist 参数
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)


    }

}

请使用这些构造函数:

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) 

    // ...
}

在主构造函数中,您需要为 topics 指定一个默认值。也给副构造函数加上context: Context参数,在调用主构造函数时传给this()