Debugging Kotlin上的OnClick适配器
我的kotlin代码在onclick适配器位置时停止。我已经调试了代码,但我仍然不确定发生了什么,为什么不起作用 这是我的类别适配器:Debugging Kotlin上的OnClick适配器,debugging,kotlin,Debugging,Kotlin,我的kotlin代码在onclick适配器位置时停止。我已经调试了代码,但我仍然不确定发生了什么,为什么不起作用 这是我的类别适配器: import android.content.Context import android.content.Intent import android.support.v7.widget.CardView import android.support.v7.widget.RecyclerView import android.view.LayoutInflate
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)
}
}
}
我已经调试了代码,在达到这一部分之前,它似乎工作得很好:
override fun onClick(view: View?) {
iOnRecyclerViewItemClickListener.onClick(view, adapterPosition)
}
}
}
我的问题是我做错了什么。在活动中实现接口,并将其作为参数传递给适配器。把它传给你的视图持有者 我给你举一个我的项目的例子
我发现了错误所在
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)
}
})
}
错误指向一个不存在的活动 你能发布异常跟踪吗?
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)
}
})
}