0

My app is an attendance for students; I create a list of the students and when I click on the student's name the picture must be checked. The problem is when I scroll down I see other students get checked too. `public class attendanceAdapter extends RecyclerView.Adapter{

private List<MyStudentsModel> studentsModels;
private AttendanceModel attModels;
private List<String> attList;
private Context context;
private DatabaseReference present, mr7laCount;
private String date = "1/6/2018";

public attendanceAdapter(Context context, List<MyStudentsModel> studentsModels){
    this.context = context;
    this.studentsModels = studentsModels;
}

@NonNull
@Override
public StudentAttHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_single_student_att_view,parent,false);
    StudentAttHolder studentAttHolder = new StudentAttHolder(view);
    attModels = new AttendanceModel();
    attList = new ArrayList<>();
    present = FirebaseDatabase.getInstance().getReference("Attendance");
    mr7laCount = FirebaseDatabase.getInstance().getReference("AllCount");
    return studentAttHolder;
}

@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull final StudentAttHolder holder, int position) {
    final String split[] = date.split("/");
    final MyStudentsModel student = studentsModels.get(position);
    holder.stu_name.setText(student.getFirstName() +" "+ student.getSecondName());

    if (student.getServedImage().equals("0") | student.getServedImage() == null){
        Glide.with(context).load(R.drawable.daria).into(holder.stu_img);
    }else {
        Glide.with(context)
                .load(Uri.parse(student.getServedImage()))
                .into(holder.stu_img);
    }

    final String id = student.getSid();

    holder.stu_img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.stu_tgb.toggle();
            if(holder.stu_tgb.isChecked()){
                attList.add(id);
                String date = split[2] + "x" + split[1] + "x" + split[0];
                String dateM = split[2] + "/" + split[1] + "/" + split[0];

                int total = getItemCount();
                int absence = total - attList.size();
                attModels.setDay(dateM);
                attModels.setPresence(String.valueOf(attList.size()));
                attModels.setAbsence(String.valueOf(absence));
                attModels.setTotal(String.valueOf(total));

                present
                        .child("Served")
                        .child(student.getAlmar7la())
                        .child(student.getAlfasl())
                        .child(split[0])
                        .child(split[1])
                        .child(split[2])
                        .setValue(attList);
                mr7laCount
                        .child("Served")
                        .child(student.getAlmar7la())
                        .child(String.valueOf(date))
                        .child(student.getAlfasl())
                        .setValue(attModels);


            } else {
                attList.remove(id);
                String date = split[2] + "x" + split[1] + "x" + split[0];
                String dateM = split[2] + "/" + split[1] + "/" + split[0];

                int total = getItemCount();
                int absence = total - attList.size();
                attModels.setDay(dateM);
                attModels.setPresence(String.valueOf(attList.size()));
                attModels.setAbsence(String.valueOf(absence));
                attModels.setTotal(String.valueOf(total));

                present
                        .child("Served")
                        .child(student.getAlmar7la())
                        .child(student.getAlfasl())
                        .child(split[0])
                        .child(split[1])
                        .child(split[2])
                        .setValue(attList);

                if(attList.size() == 0){
                    mr7laCount
                            .child("Served")
                            .child(student.getAlmar7la())
                            .child(String.valueOf(date))
                            .child(student.getAlfasl())
                            .removeValue();
                }else {
                    mr7laCount
                            .child("Served")
                            .child(student.getAlmar7la())
                            .child(String.valueOf(date))
                            .child(student.getAlfasl())
                            .setValue(attModels);
                }
            }
        }
    });



}

@Override
public int getItemCount() {
    return studentsModels.size();
}

final public class StudentAttHolder extends RecyclerView.ViewHolder{
    ImageView stu_img;
    TextView stu_name;
    ToggleButton stu_tgb;

    public StudentAttHolder(View itemView) {
        super(itemView);
        stu_name = itemView.findViewById(R.id.student_name_att);
        stu_img = itemView.findViewById(R.id.student_image_att);
        stu_tgb = itemView.findViewById(R.id.student_toggleButton);
    }
}

} `

when I checked Faustino Hausner
enter image description here

I saw Wilber Bainbridge and Hester Barley checked too enter image description here

enter image description here

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
  • 2
    check out this link :: https://stackoverflow.com/questions/32427889/checkbox-in-recyclerview-keeps-on-checking-different-items – zephyr Jun 21 '18 at 05:55
  • 1
    Possible duplicate of [CheckBox in RecyclerView keeps on checking different items](https://stackoverflow.com/questions/32427889/checkbox-in-recyclerview-keeps-on-checking-different-items) – Shree Krishna Jun 21 '18 at 06:04
  • Do you understand how RecyclerView works? You are the checking the VIew holder view which gets recycled everytime you keep scrolling, you will have to create a list of the checked indexes..I can add code here if you want. – Zach Bublil Jun 21 '18 at 06:27
  • I tried to use GridView but the problem still be. – Rofaeil Samir Jun 21 '18 at 08:12
  • you need to save the attendance state to a variable inside your MyStudentsModel. when ever user clicks on the image set it to "present" by default set the state to "absent". In your onBindViewHolder() set the green or red icon based on this value. – pop Jun 21 '18 at 14:48

0 Answers0