如何在myadapter中启动新的意图

时间:2019-03-26 19:33:56

标签: java android

我正在创建foodrecipe应用程序,并且需要有关在adapter.class中启动新意图的帮助。我知道通常您在默认构造函数中使用Context上下文,然后您就可以开始活动了。

我试图在构造函数中使用Context,但是我不能

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);
                }
            });

        }
    }

}

如果有人可以帮助我,那就太好了

1 个答案:

答案 0 :(得分:1)

您可以从context本身获得view,进行以下操作:

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

并且context应该在您的Adapter的构造函数中。

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