繁体   English   中英

RadioButton检查的状态在RecyclerView的滚动事件时自动更改

[英]RadioButton checked sate automatically changes on scrolling event of RecyclerView

我在RecyclerView项目中的RadioButton遇到问题。 根据“类型”,我更改了选中的RadioButton。 但是,在加载所有数据之后,当我单击非选中单选按钮时,它就会被选中。 但是在滚动了我刚刚单击的选中的单选按钮后,该复选框未选中。 我要检查刚刚单击的按钮。 但是它不会保持选中状态。 我如何得到想要的东西? 谁能帮帮我吗!!!!!!!

这是我的适配器类:

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);
            }
        }
    }
}

只是一个小错误。 在您的onClick方法中,更改状态时只需通知适配器即可。

 @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();
            }
        }

我可以看到,无论您做过同样的事情,在更改列表中的任何内容时通知适配器都是很重要的。 您可以使用notifyDataSetChanged()或notifyItemChanged(selectedPosition);

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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