简体   繁体   English

如何在回收器适配器中创建辅助构造函数,采用 kotlin android 工作室中的 arraylist 参数

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

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我正在尝试在 recyclerView 适配器中创建其他构造函数,但出现错误请告诉我如何在使用 kotlin android 工作室中的 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)


    }

}

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 .在主构造函数中,您需要为topics指定默认值。 Also add context: Context parameter to the secondary constructor and pass it to this() when calling the primary constructor.还要在辅助构造函数中添加context: Context参数,并在调用主构造函数时将其传递给this()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM