猿问

RadioButton 检查状态在 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();

                }

            }

        });



斯蒂芬大帝
浏览 172回答 3
3回答

猛跑小猪

单击单选按钮使用界面,对传递给回收站视图(内部活动)的原始列表进行必要的更改,然后使用 notifiDataSetChanged() 方法。

回首忆惘然

如果您仍然遇到问题,您应该创建一个模型类。

慕斯709654

只是一个小错误。在您的 onClick 方法中,在更改状态时只通知适配器。&nbsp;@Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (databaseHelper.updateStudentAttendanceData(id, userId, "P", "0") > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(context, "Marked successfully.", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; viewHolder.radioPresent.setChecked(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Notify adapter&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;attendanceArrayList.notify();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(context, "Unable to mark attendance.", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }我可以看到,您在任何地方都做了同样的事情。在更改列表中的任何内容时通知适配器很重要。您可以使用 notifyDataSetChanged() 或 notifyItemChanged(selectedPosition);
随时随地看视频慕课网APP

相关分类

Java
我要回答