我想点击Recyclerview中的cardview(item)
打开一项新活动。但我的新活动无法启动,应用程序已停止弹出
我的适配器类是
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private List<Contacts> listitems;
private Context context;
public interface OnItemClickListener{
void onItemClick(Contacts item);
}
public MyAdapter(List<Contacts> listitems, Context context) {
this.listitems = listitems;
this.context = context;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
Contacts c = listitems.get(position);
holder.t1.setText(c.getName());
holder.t2.setText(c.getNumber());
}
@Override
public int getItemCount() {
return listitems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView t1,t2;
public ImageView image;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
t1 = (TextView) itemView.findViewById(R.id.textview1);
t2 = (TextView) itemView.findViewById(R.id.textview2);
}
@Override
public void onClick(View v) {
itemView.getContext().startActivity(new Intent(context,Details.class));
}
}
}
答案 0 :(得分:0)
只需替换
itemView.getContext().startActivity(new Intent(context,Details.class));
带
context.startActivity(new Intent(context,Details.class));
答案 1 :(得分:0)
您正在使用回收站视图,然后您可以在活动中使用此功能,而不是在适配器
中recyclerview.onaddOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Activity_name.startActivity(new Intent(context,Details.class));
}
}));