首页 > 解决方案 > 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.

标签: javaandroidandroid-activityandroid-recyclerviewandroid-cardview

解决方案


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

推荐阅读