如何在onBindViewHolder中调用Context和Intent对象 [英] How to call Context and Intent objects inside onBindViewHolder
问题描述
我想在RecyclerView
中打开一个新的Activity,但是无法在其中创建Intent
对象.
I want to open a new Activity within RecyclerView
, but I can't create an Intent
object in there.
val intent1 = Intent(this,Main2Activity::class.java)
startActivity(intent1)
Android Studio警告Intent不是Context
.如何仍然在RecyclerView
Android Studio warns about Intent not being a Context
. How can I still open a new Activity inside the RecyclerView
我也尝试了下面的代码,它给出了"startActivity(intent)"行给出了错误,类型不匹配,必需的上下文,找到了Intent".
I tried also the code below,it gives "startActivity(intent)" line gives error, "type mismatch, required Context, found Intent".
此外,"this @ MainActivity"也会给出"unresolved reference @ MainActivity"错误.
Plus, also "this@MainActivity" gives "unresolved reference@MainActivity" error.
override fun onBindViewHolder(holder: Main_Menu_Holder, position: Int) {
var currentview = alldata.get(position)
}
推荐答案
您正在呼叫this
,这是您的RecyclerView
的onBindViewHolder
.您必须使用View
调用Context
:
You are calling this
which is your RecyclerView
's onBindViewHolder
. You have to call the Context
using View
:
override fun onBindViewHolder(holder: Main_Menu_Holder, position: Int, v:View) {
val intent = Intent(v.context, Main2activity::class.java)
startActivity(intent)
}
这篇关于如何在onBindViewHolder中调用Context和Intent对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!