[英]How to call Context and Intent objects inside onBindViewHolder
I want to open a new Activity within RecyclerView
, but I can't create an Intent
object in there. 我想在
RecyclerView
打开一个新的Activity,但是无法在其中创建Intent
对象。
val intent1 = Intent(this,Main2Activity::class.java)
startActivity(intent1)
Android Studio warns about Intent not being a Context
. Android Studio警告Intent不是
Context
。 How can I still open a new Activity inside the RecyclerView
我如何仍然在
RecyclerView
打开一个新的活动
I tried also the code below,it gives "startActivity(intent)" line gives error, "type mismatch, required Context, found Intent". 我也尝试了下面的代码,它给出了“ startActivity(intent)”行给出了错误,“类型不匹配,必需的上下文,找到了Intent”。
Plus, also "this@MainActivity" gives "unresolved reference@MainActivity" error. 另外,“ this @ MainActivity”也会给出“ unresolved reference @ MainActivity”错误。
override fun onBindViewHolder(holder: Main_Menu_Holder, position: Int) {
var currentview = alldata.get(position)
}
You are calling this
which is your RecyclerView
's onBindViewHolder
. 你调用
this
是您的RecyclerView
的onBindViewHolder
。 You have to call the Context
using View
: 您必须使用
View
调用Context
:
override fun onBindViewHolder(holder: Main_Menu_Holder, position: Int, v:View) {
val intent = Intent(v.context, Main2activity::class.java)
startActivity(intent)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.