Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start new Intent from RecyclerViewAdapter

I have RecyclerViewAdapter :

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyViewHolder> {



    private List<Cards> items;
    private int itemLayout;


    Context context;

    public RecycleViewAdapter(List<Cards> items, int itemLayout) {

        this.items = items;
        this.itemLayout= itemLayout;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

       View view=LayoutInflater.from(parent.getContext()).inflate(itemLayout,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {


        Cards item=items.get(position);
        holder.title.setText(item.getName());
        String cardvalue = item.getCountry();
        String cardCode = item.getCode();
        String cardCountry = item.getCountry();


    }

    @Override
    public int getItemCount() {

        return items.size();
    }
    public static class  MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView title;
        public MyViewHolder(View itemView) {

            super(itemView);

            title= (TextView) itemView.findViewById(R.id.listText);

            title.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {


        }
    }
}

and I need to start new Intent (OnCardsSelected.class) , but I cant add this line to my method onClick:

Intent intent = new Intent (this, OnCardSelected.class);

I don't understand whats I do wrong, I just need to start Intent and put some information to this intent, I read some manuals but didn't understand theirs explanation so I hope u will help me .Thanks

like image 718
Ololoking Avatar asked Feb 15 '15 16:02

Ololoking


1 Answers

Probably trying to start Activity from onClick method then use v.getContext() instead of this(which refer to onClick method context) as first parameter to Intent constructor :

Intent intent = new Intent (v.getContext(), OnCardSelected.class);
like image 189
ρяσѕρєя K Avatar answered Oct 01 '22 21:10

ρяσѕρєя K