Java 计数recyclerView中的复选框
我在recyclerView中有一个复选框列表,在该文本视图中也有 ➡ 我希望当我选中复选框时,每个复选框都应计入自己的计数,按下保存按钮后,它应将计数保存到另一个活动中,当我再次选中复选框时,它应在另一个活动中更新该计数。对于列表中的每个复选框,计数应该是单独的。 更新了Java 计数recyclerView中的复选框,java,android,checkbox,Java,Android,Checkbox,我在recyclerView中有一个复选框列表,在该文本视图中也有 ➡ 我希望当我选中复选框时,每个复选框都应计入自己的计数,按下保存按钮后,它应将计数保存到另一个活动中,当我再次选中复选框时,它应在另一个活动中更新该计数。对于列表中的每个复选框,计数应该是单独的。 更新了onBindViewHolder public void onBindViewHolder(@NonNull final StudentViewHolder holder, final int position) {
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);
}
});
}
ModelClass
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;
}
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;
}
}首先,我想感谢您帮助我解决了这个问题。我试了这么多,终于解决了 我的菜单保存按钮
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 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;
}
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);
}
这解决了我的问题……:) 首先我要感谢你帮我解决了这个问题。我试了这么多,终于解决了 我的菜单保存按钮
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 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;
}
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);
}
这解决了我的问题……:) 嗨..请分享你的代码我已经用代码更新了我的问题现在你可以看到@vahab balouchzahiI对我的代码做了一些更改,但我没有得到我想要的。你可以检查一下问题是否出在你的全局计数变量上。因此,我建议您删除它,对于计数,我建议获取考勤值,然后递增/递减,最后将其设置回原来的值。您提到了保存按钮,因此我建议为考勤创建另一个临时临时变量。当然,还有设置和获取临时变量的方法@ドラゴンハートゼクモル 一个疑问,正如你所说的“我建议先获取考勤值,然后递增/递减”,我应该在哪里初始化变量,以便递增/递减,然后将其设置回原位。嗨..请分享你的代码我已经用代码更新了我的问题,现在你可以看到@vahab balouchzahiI在我的代码中做了一些更改,但我并没有得到我想要的。你可以检查一下,问题在于你的全局计数变量。因此,我建议您删除它,对于计数,我建议获取考勤值,然后递增/递减,最后将其设置回原来的值。您提到了保存按钮,因此我建议为考勤创建另一个临时临时变量。当然,还有设置和获取临时变量的方法@ドラゴンハートゼクモル 一个疑问,正如u所说的“我建议先获取考勤值,然后递增/递减”,我应该在哪里初始化变量,以便递增/递减,然后将其设置回原来的值。已解决!不修边幅!不修边幅!不修边幅!