在onBindViewHolder中调用RecyclerView

时间:2019-09-19 22:31:04

标签: android kotlin android-recyclerview

我在“ OnBindViewHolder”中设置了“ setOnClickListener”,所以当我单击一个项目时,我会做一些事情。 但是当我单击一个项目时,它会显示如下错误,

locked

出现错误的行如下,

 java.lang.IllegalStateException: holder.itemView.myAdsRecycleView must not be null

可能是什么问题? itemview和数组似乎不为空?

完整的适配器代码如下,

holder.itemView.myAdsRecycleView.layoutManager = myLinearLayout

还在下面添加了持有人代码,

class adsRecyleAdapter(var category:ArrayList<String>) : RecyclerView.Adapter<adsViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): adsViewHolder {
        var inflater = LayoutInflater.from(parent.context).inflate(R.layout.ads_menu_single_category_layout,parent,false)
        return adsViewHolder(inflater)
    }

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

    override fun onBindViewHolder(holder: adsViewHolder, position: Int) {


        holder.itemView.txt_ads.text = category[position]


        holder.itemView.setOnClickListener(){


            if (holder.itemView.txt_ads.text.contains("Estate")){

                var array = holder.itemView.resources.getStringArray(R.array.realEstateCategory)
                var category_arraylist = ArrayList<String>(9)

                for (i in 0..4){
                    category_arraylist.add(array[i])
                }

                var myLinearLayout = LinearLayoutManager(holder.itemView.context, LinearLayoutManager.VERTICAL,false)
                holder.itemView.myAdsRecycleView.layoutManager = myLinearLayout

                var myadapter = adsRecyleAdapter(category_arraylist)
                holder.itemView.myAdsRecycleView.adapter = myadapter
            }
        }
    }
}

Itemview的代码如下,

class adsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}

0 个答案:

没有答案