简体   繁体   中英

RadioButton checked sate automatically changes on scrolling event of RecyclerView

I am facing problem with my RadioButton in RecyclerView item. According to the 'type' I changes the checked RadioButton. But after all data loaded, then when I click the non checked radio button it's get checked. But after scrolling the checked radio button which I just clicked that goes unchecked. I want the button which I just clicked to be checked. But it is doesn't stays checked. How do I get what I want? Can anyone please help me!!!!!!!

This is my Adapter Class:

public class AttendanceAdapterLocal extends  RecyclerView.Adapter<AttendanceAdapterLocal.ViewHolder>{
    private ArrayList<StudentAttendance> attendanceArrayList;
    private Context context;

    public AttendanceAdapterLocal(ArrayList<StudentAttendance> attendanceArrayList) {
        this.attendanceArrayList = attendanceArrayList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_mark_attendance, viewGroup, false);
        context = viewGroup.getContext();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
        final String type = attendanceArrayList.get(i).getType();
        final String id = attendanceArrayList.get(i).getId();
        final String name = attendanceArrayList.get(i).getName();
        final String roll = attendanceArrayList.get(i).getRoll();
        viewHolder.setData(name, roll, type, id);

        final DatabaseHelper databaseHelper = new DatabaseHelper(context);
        final User user = new AccessStorage(context).getUserDetails();
        final String userId = user.getUserId();
        viewHolder.radioPresent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (databaseHelper.updateStudentAttendanceData(id, userId, "P", "0") > 0) {
                    Toast.makeText(context, "Marked successfully.", Toast.LENGTH_LONG).show();
                    viewHolder.radioPresent.setChecked(true);
                } else {
                    Toast.makeText(context, "Unable to mark attendance.", Toast.LENGTH_LONG).show();
                }
            }
        });
        viewHolder.radioAbsent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (databaseHelper.updateStudentAttendanceData(id, userId, "A", "0") > 0) {
                    Toast.makeText(context, "Marked successfully.", Toast.LENGTH_LONG).show();
                    viewHolder.radioAbsent.setChecked(true);
                } else {
                    Toast.makeText(context, "Unable to mark attendance.", Toast.LENGTH_LONG).show();
                }
            }
        });
        viewHolder.radioNone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (databaseHelper.updateStudentAttendanceData(id, userId, "NA", "0") > 0) {
                    Toast.makeText(context, "Marked successfully.", Toast.LENGTH_LONG).show();
                    viewHolder.radioNone.setChecked(true);
                } else {
                    Toast.makeText(context, "Unable to mark attendance.", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

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

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        // Widgets
        private TextView textName;
        private RadioGroup radioGroup;
        private RadioButton radioPresent, radioAbsent, radioNone;
        // Vars
        private View view;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;

            textName = view.findViewById(R.id.textName);
            radioGroup = view.findViewById(R.id.radioGroup);
            radioPresent = view.findViewById(R.id.radioPresent);
            radioAbsent = view.findViewById(R.id.radioAbsent);
            radioNone = view.findViewById(R.id.radioNone);
        }

        private void setData(String name, String roll, final String type, final String id) {
            textName.setText(roll + ". " + name);

            if (type.equals("P")) {
                radioPresent.setChecked(true);
            } else if(type.equals("A")) {
                radioAbsent.setChecked(true);
            }else if(type.equals("NA")) {
                radioNone.setChecked(true);
            }
        }
    }
}

Just a small mistake . In your onClick method , while changing the state just notify the adapter .

 @Override
        public void onClick(View v) {
            if (databaseHelper.updateStudentAttendanceData(id, userId, "P", "0") > 0) {
                Toast.makeText(context, "Marked successfully.", Toast.LENGTH_LONG).show();
                viewHolder.radioPresent.setChecked(true);
            //Notify adapter 
             attendanceArrayList.notify();
            } else {
                Toast.makeText(context, "Unable to mark attendance.", Toast.LENGTH_LONG).show();
            }
        }

I can see , everywhere you have done the same thing .Its important to notify the adapter while changing anything in the list . You either use notifyDataSetChanged() or notifyItemChanged(selectedPosition);

如果仍然遇到问题,则应创建一个Model类。

单击单选按钮上的“使用接口”,在要在回收器视图(内部活动)中传递的原始列表中进行必要的更改,然后使用notifiDataSetChanged()方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM