简体   繁体   English

从 recyclerview 适配器开始新活动

[英]Starting new activity from recyclerview adapter

I am reading data from firebase real time database to a recyclerview and on the recyclerview item there a button to start a new activity.我正在将数据从 firebase 实时数据库读取到 recyclerview,在 recyclerview 项目上有一个按钮可以开始新的活动。 I have this onClick method我有这个 onClick 方法

public void bookBtnOnClick(final int position){
        mBookButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(new Intent(this,ReservationForm.class));
                }
        });
    } 

But it cannot resolve intent constructer但它无法解析意图构造函数

而不是这个尝试mContext因为我猜你是在 Adapter 类中尝试它

  mContext.startActivity(new Intent(mContext,ReservationForm.class));

'this'改为'mContext'

mContext.startActivity(new Intent(mContext,ReservationForm.class));

try-->试试-->

Intent intent =  new Intent(context, ReservationForm.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mcontext.startActivity(intent);

Adding more to @S.ambika's answer.为@S.ambika 的回答添加更多内容。

Change this to mContext when creating Intent .改变thismContext创建时Intent

mContext.startActivity(new Intent(mContext,ReservationForm.class));

As when you use this , it will give the current context.当您使用this ,它将提供当前上下文。 In your case this is returning the context of onClick and for starting a new Activity, we need the context of Activity .在您的情况下, this是返回onClick的上下文,为了启动新的 Activity,我们需要Activity的上下文。

try this:尝试这个:

public void bookBtnOnClick(final int position){
     mContext.startActivity(new Intent(context, ReservationForm.class));
} 

How's about this这个怎么样

Intent intent = new Intent (v.getContext(), ReservationForm.class);
v.getContext().startActivity(intent);
Define Context mContext;  on starting of adapter and in constructor use this.mcontext=mcontext;

mContext.startActivity(new Intent(mContext,ReservationForm.class));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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