skip to Main Content

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


  1. you already have int position, just make it final and then you may use it inside onClick

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        ...
        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);
                intent.putExtra("position", position);
                // would be better to pass item id or name, much better approach
                intent.putExtra("itemId", myMovieDataList.getMovieId());
                v.getContext().startActivity(intent);
    
            }
        });
    }
    
    Login or Signup to reply.
  2. Answer by @snachman is perfectly right. But, it is not preferred by google. Use the method given below.

    1. Create a new interface with name RecyclerViewItemClickListener.java
    2. Add method void onClick(); to it;
    3. Add that to the constructor of the adapter.
    4. After adding the constructor, add it to your activity by implementing the interface
    5. Add it in the activity by new RecyclerViewItemClickListener
    6. Now in the activity, add the code to start the activity.
      Hope it helps 😀
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search