public class VendorAdapter extends RecyclerView.Adapter<VendorAdapter.MyViewHolder> {
private List<Vendor> vendorList;
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView name, rating, address;
RelativeLayout rl;
Context con;
public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.tv1);
address = (TextView) view.findViewById(R.id.tv2);
rating = (TextView) view.findViewById(R.id.txtstar);
rl=(RelativeLayout)view.findViewById(R.id.parentLayout);
itemView.setClickable(true);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent (v.getContext(), StartActivity.class);
con.startActivity(intent);
}
}
public VendorAdapter(List<Vendor> vendorList,Context con) {
this.vendorList = vendorList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_layout, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Vendor movie = vendorList.get(position);
/*holder.name.setText(movie.getTitle());
holder.address.setText(movie.getGenre());
holder.rating.setText(movie.getYear());*/
}
@Override
public int getItemCount() {
return vendorList.size();
}
}
我想在回收站视图的每个项目点击上打开相同的活动,但是我这样做的方式,我在这行-con.startActivity(intent)上得到NULL POINTER; 请帮我解决一下这个。 ##
答案 0 :(得分:3)
您可以使用您点击的视图中的上下文。
Intent intent = new Intent (v.getContext(), StartActivity.class);
v.getContext().startActivity(intent);
或者在构造函数中分配上下文
this.con = con;
答案 1 :(得分:0)
将Context con;
从MyViewHolder class
移至VendorAdapter
以下
public class VendorAdapter ...{
private List<Vendor> vendorList;
Context con; // move con to here
public class MyViewHolder ...
public TextView name, rating, address;
RelativeLayout rl;
...
}
然后在con
构造函数中接收VendorAdapter
,就像这样
public VendorAdapter(List<Vendor> vendorList,Context con) {
this.vendorList = vendorList;
this.con = con; // add this
}
希望这个帮助