简体   繁体   中英

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. i know normally you use Context context in de default constructer and then you can startactivity.

and i tried to use Context in my constructer but i can't

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 :

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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