0

Peace

I'm trying to add a button that will open a new window in RecyclerView, but it gives me an error in red

Intent intent = new Intent(this, MainActivity.class); startActivity(intent);

bold marks a red line^^^

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;
    ArrayList<Fly> list;
    public static int idB = 0;

    public MyAdapter(Context context, ArrayList<Fly> list) {
        this.context = context;
        this.list = list;
    }



    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view =  LayoutInflater.from(context).inflate(R.layout.userentry,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        Fly fly = list.get(position);
        holder.name.setText(fly.getNamefly());
        holder.datefly.setText(fly.getDatefly());
        holder.hourfly.setText(fly.getHourfly());
        holder.contentfly.setText(fly.getContentfly());
        holder.pricefly.setText(fly.getpricefly());
        holder.buttonBUYR.setText(fly.getbuttonBUY());


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView name,datefly,hourfly,contentfly,pricefly,buttonBUYR;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.textname);
            datefly = itemView.findViewById(R.id.textdate);
            hourfly = itemView.findViewById(R.id.texthour);
            contentfly = itemView.findViewById(R.id.textcontent);
            pricefly = itemView.findViewById(R.id.textprice);
            buttonBUYR = itemView.findViewById(R.id.buttonBUY);
            itemView.findViewById(R.id.buttonBUY).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent = new Intent(this, MainActivity.class);
                    startActivity(intent);

                }
            });


        }
    }
}

I have a screen with rows in which each row has properties such as price name and button I want every button that is pressed to do an action

And I get this error

Cannot resolve constructor 'Intent(anonymous android.view.View.OnClickListener, java.lang.Class<com.example.newp.MainActivity>)'

and

'startActivity(android.content.Context, android.content.Intent, android.os.Bundle)' in 'androidx.core.content.ContextCompat' cannot be applied to '(android.content.Intent)'
ADM
  • 20,406
  • 11
  • 52
  • 83
yosef
  • 1
  • 2
  • I don't know much about JAVA, but In kotlin, we invoke the click listener inside the init{here} – Haris Dec 10 '22 at 15:46

1 Answers1

1

I'm not able to compile your code but since you have a Context parameter, I'll assume your'e passing an instance of your activity to it when you initialized your adapter, so change your Intent declaration like this, and also use it to call startActivity

 Intent intent = new Intent(context, MainActivity.class);
 context.startActivity(intent);
z.g.y
  • 5,512
  • 4
  • 10
  • 36