Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to startActivity from Recycler View Adapter [duplicate]

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

}

Unable to startActivity from Recycler View Adapter

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. ##

like image 784
Siddharth Mishra Avatar asked Apr 14 '16 10:04

Siddharth Mishra


People also ask

What is the relationship between RecyclerView adapter and RecyclerView ViewHolder?

ViewHolder is not bound to an item or the given RecyclerView. Adapter is not part of this Adapter (if this Adapter merges other adapters).

What is the use of onBindViewHolder in Android?

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.

What is RecyclerView adapter in Android?

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.

What is onCreateViewHolder?

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 .


Video Answer


1 Answers

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;
like image 131
Tom Sabel Avatar answered Nov 10 '22 01:11

Tom Sabel