简体   繁体   English

如何在onBindViewHolder中调用Context和Intent对象

[英]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是您的RecyclerViewonBindViewHolder 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM