我在 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(); } } });
3 回答
慕斯709654
TA贡献1840条经验 获得超5个赞
只是一个小错误。在您的 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);
添加回答
举报
0/150
提交
取消