Android Kotlin在回收者视图中对点击事件的新意图 [英] Android Kotlin New intent on click event from recycler view
问题描述
我刚刚开始与Kotlin学习android开发.
I've just started learning android development with Kotlin.
我有一个列出物品的回收站视图.
I have a recycler view that lists items.
我正在尝试为商品创建onClick事件,然后启动新的意图并传递商品ID.
I'm trying to create onClick event for the item and then start a new intent and pass along the item id.
尝试实例化Intent时出现错误:以下任何函数均不能使用提供的参数来调用".我认为是因为我无法访问视图的上下文?
I'm getting an error when trying to instantiate the Intent "None of the following functions can be called with the arguments supplied". I think it's because I cannot access the context of the view?
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.id = events[position][0]
holder.title.text = events[position][1]
if (events[position][2] == events[position][3]) {
holder.date.text = events[position][2]
} else {
holder.date.text = events[position][2] + " - " + events[position][3]
}
holder.itemView.setOnClickListener {
v: View -> Unit
Log.d(TAG, "onItemClick for ID: " + holder.id)
val intent = Intent(this, EventDetail::class.java)
intent.putExtra("id", holder.id)
startActivity(intent)
}
}
推荐答案
您需要一个 context
作为意图,以便可以通过 activity
或 fragment来获取它与该
并将其传递给您的意图,或者您可以从这样的膨胀布局中的任何视图中获取它 adapter
关联的
you need a context
for intent so you can get it form the activity
or fragment
that are associated with this adapter
and pass it to your intent or you can get it from any view in your inflated layout like this
val context=holder.title.context
val intent = Intent( context, EventDetail::class.java)
context.startActivity(intent)
或者您可以使您的 activity
或 fragment
实现它的侦听器,并且可以在其回调中像这样创建您的意图
or you can make a listener that your activity
or fragment
implement it and you can on it's callback create your intent like this
val intent = Intent(this @ YourCalssname,EventDetail :: class.java)
这篇关于Android Kotlin在回收者视图中对点击事件的新意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!