简体   繁体   English

如何在myadapter中启动新的意图

[英]How to start new intent in myadapter

I'm creating a foodrecipe app and i need help with starting a new intent in my adapter.class. 我正在创建foodrecipe应用程序,并且需要有关在adapter.class中启动新意图的帮助。 i know normally you use Context context in de default constructer and then you can startactivity. 我知道通常您在默认构造函数中使用Context上下文,然后就可以开始活动了。

and i tried to use Context in my constructer but i can't 我试图在我的构造函数中使用上下文,但是我不能

MyAdapter MyAdapter

public class Myadapter extends FirestoreRecyclerAdapter<Note, Myadapter.MyHolder> {


    public Myadapter(@NonNull FirestoreRecyclerOptions<Note> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull MyHolder holder, int position, @NonNull Note model) {
        holder.textViewname.setText(model.getNamerecipe());
        holder.textViewinfo.setText(model.getInforecipe());

        final String getname = holder.textViewname.getText().toString();




    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
        return new MyHolder(v);
    }

    class MyHolder extends RecyclerView.ViewHolder{
        TextView textViewinfo;
        TextView textViewname;
        ImageView imageView;
        Context context;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            textViewinfo = itemView.findViewById(R.id.text_view_desciption);
            textViewname = itemView.findViewById(R.id.text_view_title);
            imageView = itemView.findViewById(R.id.Imageview);



            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, Recipeinfo.class);
                    context.startActivity(intent);
                }
            });

        }
    }

}

If someone can help me it would be great, 如果有人可以帮助我,那就太好了,

You can get the context from the view itself, doing : 您可以从view本身获取context ,方法是:

itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), Recipeinfo.class);
            context.startActivity(intent);
        }
    });

And context should be in the constructor of your Adapter . context应该在Adapter的构造函数中。

private Context mContext;
public Myadapter(@NonNull FirestoreRecyclerOptions<Note> options, Context context) {
super(options);
this.mContext = context;
}

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

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