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();
}
}
I want to open same activity on every item click of recycler view,but the way i am doing it,i am getting NULL POINTER on this line-con.startActivity(intent); Please help me with this. ##
ViewHolder is not bound to an item or the given RecyclerView. Adapter is not part of this Adapter (if this Adapter merges other adapters).
It is called by RecyclerView to display the data at the specified position. This method is used to update the contents of the itemView to reflect the item at the given position.
The RecyclerView is a ViewGroup that renders any adapter-based view in a similar way. It is supposed to be the successor of ListView and GridView. One of the reasons is that RecyclerView has a more extensible framework, especially since it provides the ability to implement both horizontal and vertical layouts.
onCreateViewHolder only creates a new view holder when there are no existing view holders which the RecyclerView can reuse. So, for instance, if your RecyclerView can display 5 items at a time, it will create 5-6 ViewHolders , and then automatically reuse them, each time calling onBindViewHolder .
You can use the context from the View you clicked on.
Intent intent = new Intent (v.getContext(), StartActivity.class);
v.getContext().startActivity(intent);
Or assign the context in your constructor
this.con = con;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With