Kotlin上的OnClick适配器

时间:2018-11-30 11:56:46

标签: debugging kotlin

当onclick adapterPosition时,我的kotlin代码制动。我已经调试了代码,但仍不确定发生了什么以及为什么不起作用。

这是我的类别适配器:

import android.content.Context
import android.content.Intent
import android.support.v7.widget.CardView
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import com.google.android.gms.common.internal.service.Common
import com.letsbuildthatapp.kotlinmessenger.Quizz.Model.Category
import com.letsbuildthatapp.kotlinmessenger.Quizz.Model.Interface.IOnRecyclerViewItemClickListener
import com.letsbuildthatapp.kotlinmessenger.Quizz.Model.Question

import com.letsbuildthatapp.kotlinmessenger.R



class CategoryAdapter(internal var context: Context,
                      internal var categoryList: List<Category>):

RecyclerView.Adapter<CategoryAdapter.MyViewHolder>() {
    //This is correct
    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): MyViewHolder {
        val itemView = LayoutInflater.from(context).inflate(R.layout.layout_category_item, parent, false)
        return MyViewHolder(itemView)
    }

    // this is correct
    override fun getItemCount(): Int {
        return categoryList.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.txt_category_name.text = categoryList[position].name
        holder.setiOnRecyclerViewItemClickListener(object : IOnRecyclerViewItemClickListener {
            override fun onClick(view: View?, position: Int) {
                // this is to direct user to the question List
                com.letsbuildthatapp.kotlinmessenger.Quizz.Model.Common.Common.selectedCategory = categoryList[position]
                val intent = Intent(context, Question::class.java)
                context.startActivity(intent)
            }

        })

    }

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

        internal var txt_category_name: TextView
        internal var card_category: CardView
        internal lateinit var iOnRecyclerViewItemClickListener: IOnRecyclerViewItemClickListener


        fun setiOnRecyclerViewItemClickListener(iOnRecyclerViewItemClickListener: IOnRecyclerViewItemClickListener) {
            this.iOnRecyclerViewItemClickListener = iOnRecyclerViewItemClickListener
        }


        init {
            txt_category_name = itemView.findViewById(R.id.txt_category_name) as TextView
            card_category = itemView.findViewById(R.id.card_category) as CardView

            itemView.setOnClickListener(this)
        }


        override fun onClick(view: View?) {
            iOnRecyclerViewItemClickListener.onClick(view, adapterPosition)

        }
    }
}

这是IonRecyclerViewItemClickLister

interface IOnRecyclerViewItemClickListener {

    fun onClick(view: View?, position:Int)
}

我已经调试了代码,在到达这一部分之前,它似乎运行良好:

        override fun onClick(view: View?) {
            iOnRecyclerViewItemClickListener.onClick(view, adapterPosition)

        }
    }
}

我的问题是我在做错什么。

2 个答案:

答案 0 :(得分:0)

在您的活动中实现您的界面,并将其作为参数传递给适配器。通过后,您的视图持有人。

我将以我的项目为例。

My interface

MainActivity - Where I implement the Interface

My adapter

答案 1 :(得分:0)

我发现了错误所在

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    holder.txt_category_name.text = categoryList[position].name
    holder.setiOnRecyclerViewItemClickListener(object : IOnRecyclerViewItemClickListener {
        override fun onClick(view: View?, position: Int) {
            // this is to direct user to the question List
            com.letsbuildthatapp.kotlinmessenger.Quizz.Model.Common.Common.selectedCategory = categoryList[position]
            val intent = Intent(context, Question::class.java)
            context.startActivity(intent)
        }

    })

}

错误表明该活动不存在。