Lajos Neto Lajos Neto - 1 year ago 122
Android Question

Get card data and pass to intent when clicked

I'm working with

and got stuck in a little problem. I have a
where i am displaying some cards.

The problem is how to pass a card variable to another
when it is clicked?

I Have the following View Holder:

public class DisciplinaViewHolder extends RecyclerView.ViewHolder {

protected TextView disciplinaNome;
protected TextView disciplinaCodigo;
protected TextView disciplinaCriador;
protected TextView disciplinaProfessor;
protected TextView disciplinaPeriodo;
protected CardView card;
protected ImageButton calendarButton;

public DisciplinaViewHolder(View itemView) {

disciplinaNome = (TextView) itemView.findViewById(;
disciplinaCodigo = (TextView) itemView.findViewById(;
disciplinaCriador = (TextView) itemView.findViewById(;
disciplinaProfessor = (TextView) itemView.findViewById(;
disciplinaPeriodo = (TextView) itemView.findViewById(;

calendarButton = (ImageButton) itemView.findViewById(;
calendarButton.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) {
Intent intent = new Intent(v.getContext(), CalendarActivity.class);

card = (CardView) itemView;

An example would be: Supposing I have 3 cards:

>Card1(name = "First Card"),
>Card2(name = "Second Card"),
>Card3(name = "Third Card").

When I press a
inside one of these cards, I want to get the name variable from the pressed card and pass it inside a
on the
method. So, if I press the
inside Card1, I would pass the value "First Card" to my next

Thank you in advance.

Answer Source

Use extras for that :

Intent intent = new Intent(v.getContext(), CalendarActivity.class);
intent.putExtra("extraName", extraValue);

please notice you will get issues launching the good activity with your method. Since RecyclerViews recycle list items, you need to set the click listener in the onBindViewHolder method and not in the ViewHolder constructor.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download