I created six of CardView and linked them to RecyclerView , how when press on cardview postion[2]
I want to make every card view and guest a move to another activity
this my code.
public class MyMovieAdapter extends RecyclerView.Adapter<MyMovieAdapter.ViewHolder> {
MyMovieData[] myMovieData;
Context context;
public MyMovieAdapter(MyMovieData[] myMovieData,MainActivity activity) {
this.myMovieData = myMovieData;
this.context = activity;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.movie_item_list,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final MyMovieData myMovieDataList = myMovieData[position];
holder.textViewName.setText(myMovieDataList.getMovieName());
holder.textViewDate.setText(myMovieDataList.getMovieDate());
holder.movieImage.setImageResource(myMovieDataList.getMovieImage());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, myMovieDataList.getMovieName(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(v.getContext(), MainActivity2.class);//////////////// //this line //////////////////////// I want to position id
v.getContext().startActivity(intent);
}
});
}
2
Answers
you already have
int position
, just make itfinal
and then you may use it insideonClick
Answer by @snachman is perfectly right. But, it is not preferred by google. Use the method given below.
RecyclerViewItemClickListener.java
void onClick();
to it;new RecyclerViewItemClickListener
Hope it helps 😀