java - 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.
解决方案
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);
}
}
});
推荐阅读
- javascript - Python中是否有任何运算符来检查和比较类型和值?
- python-3.x - 如何为特定的 python 版本安装 pip3
- python - 如何在 IDLE 上运行特定于命令提示符的 python 库?
- node.js - 将 Google Cloud SQL Server 部署到 Heroku
- oracle - 如何从 Oracle 和 Linq 中的列中获取最小未使用数?
- selenium - 如何在 Selenium 负载测试期间设置最大并发用户数?负载测试并行打开的并发用户比我设置的多
- c - 条件 !fork() 和 fork() > 0 有什么区别?
- flutter - dart 构造函数中的冒号是什么意思?
- laravel - Laravel5 事件“删除”在 Model::whereIn() 上不起作用
- json - 从 json 填充选择器,SwiftUI