如何在onBindViewHolder中调用Context和Intent对象 [英] How to call Context and Intent objects inside onBindViewHolder

查看:946
本文介绍了如何在onBindViewHolder中调用Context和Intent对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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,这是您的RecyclerViewonBindViewHolder.您必须使用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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆