Hugo Becker
Hugo Becker

Reputation: 43

Open 1 different activity for each cardview

I'm trying to figure how to go to another activity when I click on one item of a cardview (with recyclerview). I searched on internet and this website and tried several options, but it isn't working (I tried only for position 0, the others will be done the same). Here, with the card 0 i try to go to "selection_niveau".

1st try : inside OnClick:

public class adapter_categorie_solo extends RecyclerView.Adapter<adapter_categorie_solo.ViewHolder> {

    List<String> titles;
    List<Integer> images;
    LayoutInflater inflater;

    public adapter_categorie_solo(Context ctx, List<String> titles, List<Integer> images){
        this.titles = titles;
        this.images = images;
        this.inflater = LayoutInflater.from(ctx);
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.cardview_solo_categorie,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.title.setText(titles.get(position));
        holder.gridIcon.setImageResource(images.get(position));

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView gridIcon;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.textView2);
            gridIcon = itemView.findViewById(R.id.imageView2);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), "Clicked -> " + getAdapterPosition(), Toast.LENGTH_SHORT).show();

                    if (getAdapterPosition()==0)
                    Intent intent = new Intent (this,selection_niveau.class);
                }
            });
        }
    }
}

I get this error :

error: variable declaration not allowed here
                    Intent intent = new Intent (this,selection_niveau.class);

2nd try : inside onBindViewHolder

public class adapter_categorie_solo extends RecyclerView.Adapter<adapter_categorie_solo.ViewHolder> {

    List<String> titles;
    List<Integer> images;
    LayoutInflater inflater;

    public adapter_categorie_solo(Context ctx, List<String> titles, List<Integer> images){
        this.titles = titles;
        this.images = images;
        this.inflater = LayoutInflater.from(ctx);
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.cardview_solo_categorie,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.title.setText(titles.get(position));
        holder.gridIcon.setImageResource(images.get(position));
        Intent intent;
        if(position == 0)
            intent = new Intent(this, selection_niveau.class);

        holder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override public void onClick(View v){
                Context.startActivity(intent);
            }
        });

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView gridIcon;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.textView2);
            gridIcon = itemView.findViewById(R.id.imageView2);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), "Clicked -> " + getAdapterPosition(), Toast.LENGTH_SHORT).show();

                }
            });
        }
    }
}

And I get this error :

error: no suitable constructor found for Intent(adapter_categorie_solo,Class<selection_niveau>)
            intent = new Intent(this, selection_niveau.class);
                     ^
    constructor Intent.Intent(String,Uri) is not applicable
      (argument mismatch; adapter_categorie_solo cannot be converted to String)
    constructor Intent.Intent(Context,Class<?>) is not applicable
      (argument mismatch; adapter_categorie_solo cannot be converted to Context)

I tried to change "this" to only "context", but it doesn't work either.

These are some of the solutions I found on internet and here, but none is working and I can't really understand why as being a beginner.

Upvotes: 0

Views: 54

Answers (3)

rahat
rahat

Reputation: 2056

Intent constructor requires a context as the first argument, but this in the ViewHolder refers to the instance of ViewHolder, So you need to get context from the view or use the context reference you got in the constructor of the adapter_categorie_solo.

public class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView gridIcon;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.textView2);
            gridIcon = itemView.findViewById(R.id.imageView2);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), "Clicked -> " + getAdapterPosition(), Toast.LENGTH_SHORT).show();

                    if (getAdapterPosition()==0)
                    {
                        Intent intent = new Intent (ctx,selection_niveau.class);
                        ctx.startActivity(intent);
                    }
                }
            });
        }
    }

Using switch instead of if will be an optimal approach if you want to start different activity based on click position.

Upvotes: 0

Zain
Zain

Reputation: 40878

Inside the onClick() callback this keyword will points to the anonymous inner class of the View.OnClickListener; so you need to change to a context by using v.getContext() instead.

Apply this by replacing itemView.setOnClickListener with:

itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(), "Clicked -> " + getAdapterPosition(), Toast.LENGTH_SHORT).show();

        if (getAdapterPosition()==0) {
            Intent intent = new Intent (v.getContext(), selection_niveau.class);
        }
    }
});

Upvotes: 2

Rajan Kali
Rajan Kali

Reputation: 12953

use context in Intent instead of this

if(position == 0)
     intent = new Intent(context, selection_niveau.class);

Upvotes: 0

Related Questions