java - 计算 recyclerView 中选中的复选框
问题描述
我在 recyclerView 中有一个复选框列表,其中 textView 也在那里。
➡我希望当我选中复选框时,每个复选框都应该计算自己的计数,并且在按下保存按钮后它应该将计数保存在另一个活动中当我再次选中复选框时,它应该在另一个中更新该计数活动。对于列表中的每个复选框,计数应该是单独的。
更新onBindViewHolder
public void onBindViewHolder(@NonNull final StudentViewHolder holder, final int position) {
final ListOfNames currentItems = mListOfNames.get(position);
holder.studentName.setText(currentItems.getStudentName());
holder.attendence.setText(currentItems.getTempAttendances());
holder.mCheckedBox.setChecked(currentItems.getTemporaryChecked());
holder.mCheckedBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int attendances=0 ;
CheckBox checkBox = (CheckBox) v;
currentItems.setTempChecked(checkBox.isChecked());
if (checkBox.isChecked()) {
attendances++;
}
currentItems.setTempAttendances(attendances);
}
});
}
模型类
public class ListOfNames {
private String studentName;
private boolean isChecked;
private transient boolean tempChecked;
private transient String tempAttendances;
private String attandance;
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof ListOfNames) {
ListOfNames other = (ListOfNames) obj;
return Objects.equals(this.studentName, other.studentName);
}
return super.equals(obj);
}
public ListOfNames(String nStudentName) {
studentName = nStudentName;
}
public String getStudentName() {
return studentName;
}
public void setTempChecked(boolean checked) {
tempChecked = checked;
}
public boolean getTemporaryChecked() {
return tempChecked;
}
public void checkedToTempChecked() {
tempChecked = isChecked;
}
public void tempCheckedToChecked() {
isChecked = tempChecked;
}
public void setTempAttendances(int attendant) {
tempAttendances = String.valueOf(attendant);
}
public String getTempAttendances() {
return tempAttendances;
}
public void tempAttendanceToAttendance() {
attandance = tempAttendances;
}
public void attendanceToTempAttendance() {
tempAttendances = attandance;
}
}
解决方案
首先我想说谢谢你帮助我解决了这个问题。我尝试了很多,最后我解决了。
我的菜单保存按钮
case R.id.saveButton:
//codes..
for (ListOfNames current : listOfNames
) {
current.tempCheckedToChecked();
current.tempAttendanceToAttendance(); // using transient variable in my model class
//this method helps to setting temporary to default value.
}
saveData();
saveAllData();
Toast.makeText(this, "Saved!!", Toast.LENGTH_SHORT).show();
return true;
}
onBindViewHolder
public void onBindViewHolder(@NonNull final StudentViewHolder holder, final int position) {
final ListOfNames currentItems = mListOfNames.get(position);
holder.attendence.setText(currentItems.getTempAttendances());
holder.mCheckedBox.setChecked(currentItems.getTemporaryChecked());
holder.mCheckedBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tempAttendances = currentItems.getTempAttendances();
CheckBox checkBox = (CheckBox) v;
currentItems.setTempChecked(checkBox.isChecked());
if (checkBox.isChecked()) {
tempAttendances++;
}
currentItems.setTempAttendances(tempAttendances);
}
});
}
模型类
public class ListOfNames {
private String studentName;
private boolean isChecked;
private String OverallAttendce;
private transient boolean tempChecked;
private transient int tempAttendances;
private int attendance;
public ListOfNames(String nStudentName) {
studentName = nStudentName;
}
//codes...
public void setTempAttendances(int attendant) {
tempAttendances = (attendant);
}
public int getTempAttendances() {
return tempAttendances;
}
public void tempAttendanceToAttendance() {
attendance = tempAttendances;
}
public void attendanceToTempAttendance() {
tempAttendances = attendance;
}
}
onBindViewHolder
我正在检索学生列表出勤率的另一项活动...
public void onBindViewHolder(@NonNull OverallAttendances_Adapter.thisViewHolder holder, int position) {
ListOfNames currentItems = overallList.get(position);
//Here I used attendance variable as of `String` type, because in my model class `tempAttendances` is of type `int`. that's why i coverted it into a String.
String attendance = String.valueOf(currentItems.getTempAttendances());
holder.stAttendances.setText(attendance);
}
这解决了我的问题..... :)
推荐阅读
- html - 为什么我的图像不显示?感觉我什么都试过了
- swift - 使用区分子节点从 Firebase 检索节点
- java - 插入外键约束
- c++ - 为什么 std::vector 允许对其包含的类型使用可抛出的移动构造函数?
- arrays - 通过道具传递数据并使用array.map不起作用
- mysql - 从两个值中获取最大值
- gf - 语法框架:“线性化类型字段不能为 Int”;如何用算术表达式编写语法的具体语法?
- sql - 表查询的多个分组依据
- mysql - 在 MySQL 中解析自定义日期格式
- python - 理解函数 forward() 和 getUnconnectedOutLayers()