'On Clicking one button, it Triggers another button in recycle view

I'm currently working on a attandance management Android app. In my recyclerview there are list of student detail being populated dynamically from database. When i click the first button in the list, it also change the button colour of the 11th button of the list Like wise to 2nd button click changes the colour of 12th View button

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    Context context;`enter code here`
    List<StudentDetails> MainImageUploadInfoList;


    public RecyclerViewAdapter(Context context, List<StudentDetails> TempList) {

        this.MainImageUploadInfoList = TempList;

        this.context = context;

    }

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

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

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

        StudentDetails studentDetails = MainImageUploadInfoList.get(position);

        holder.StudentNameTextView.setText(studentDetails.getName());

        holder.StudentNumberTextView.setText(studentDetails.getPhoneNumber());
        holder.st.setText(studentDetails.getRollno());
        holder.cardView.setCardBackgroundColor(getcolor(position));


    }


    private int getcolor(int position) {
       
        return Color.parseColor("#" + Integer.toHexString(ContextCompat.getColor(context, R.color.normal)));
    }

    @Override
    public int getItemCount() {

        return MainImageUploadInfoList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView StudentNameTextView;
        public TextView StudentNumberTextView;
        public TextView st;
        DatabaseReference databaseReference;
        String se,wh,su,d;
        Button b;


       

      CardView cardView;
        public ViewHolder(View itemView) {

            super(itemView);
            itemView.setOnClickListener(this);

            StudentNameTextView = (TextView) itemView.findViewById(R.id.ShowStudentNameTextView);

            StudentNumberTextView = (TextView) itemView.findViewById(R.id.ShowStudentNumberTextView);
            st = (TextView) itemView.findViewById(R.id.studentrollno);
            cardView=itemView.findViewById(R.id.cardview1);
            se=Teacher.getInstance().getSe();
            wh=Teacher.getInstance().getWh();
            su=Teacher.getInstance().getSu();
            d=Teacher.getInstance().getD();
            b=itemView.findViewById(R.id.button3);


            itemView.findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View view  ) {
                    int position=getAdapterPosition();
                    String message = String.valueOf(position) ;
                    databaseReference= FirebaseDatabase.getInstance().getReference("attandance").child(wh).child(se).child(su).child(d).child(message);
                    databaseReference.setValue("p");
                    itemView.findViewById(R.id.button3).setBackgroundColor(Color.GREEN);
                    itemView.findViewById(R.id.button4).setEnabled(false);
                    itemView.findViewById(R.id.button3).setEnabled(false);
                    Toast.makeText( context,"position is "+String.valueOf(position), Toast.LENGTH_SHORT).show();
                }
            });
            itemView.findViewById(R.id.button4).setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(final View view  ) {
                    int position=getAdapterPosition();
                    String message = String.valueOf(position) ;
                    databaseReference= FirebaseDatabase.getInstance().getReference("attandance").child(wh).child(se).child(su).child(d).child(message);
                    databaseReference.setValue("A");
                    itemView.findViewById(R.id.button4).setBackgroundColor(Color.RED);
                    itemView.findViewById(R.id.button3).setEnabled(false);
                    itemView.findViewById(R.id.button4).setEnabled(false);
                }
            });
        }



        @Override
        public void onClick(View view) {


           // int position=this.getAdapterPosition();
          //  Intent intent=new Intent(context,mark.class);
           // String message = String.valueOf(position);
           // intent.putExtra("attan",message);
          //  context.startActivity(intent);
           // Intent intent = new Intent(context,mark.class);
            //String message = String.valueOf(position) ;
            //intent.putExtra("attan",message);
            //context.startActivity(intent);
           // Toast.makeText( context,"position is "+String.valueOf(position), Toast.LENGTH_SHORT).show();

        }
    }

    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source