【发布时间】:2019-03-17 20:22:48
【问题描述】:
我有 60 个项目的 RecyclerView。那个项目有 RadioGroup 并且在这个有两个单选按钮
我也使用 DataBinding
我有 FabButton,如果单击此按钮,我将转到结果活动,但如果选中所有 RadioGroup :)
我写了这样的代码,它可以工作,但有一些错误......当我点击第 60 项时,即使不检查 59 项,我将转到结果活动。
为什么?
这是我的代码:
//Select All Radio Group
public boolean allSelected() {
boolean allChecked = true;
for (Question question : questions) {
allChecked = question.getSelectedId() != 0;
}
return allChecked;
}
//Card background if Uncheck the question
private void showHideErrorBackground(boolean show) {
for (Question question : questions) {
question.setShowErrorBackground(show);
}
mbtiQuestAdapter.notifyDataSetChanged();
}
我喜欢这个方法:
if (allSelected()) {
Intent intent = new Intent(MbtiQuestionActivity.this, ResultActivity.class);
startActivity(intent);
} else {
snack bar =
Snackbar.make(coordinator, R.string.check_all_ques_err, Snackbar.LENGTH_SHORT)
.setAction(R.string.snack_find_unchecked_ques, new View.OnClickListener() {
@Override
public void onClick(View view) {
showHideErrorBackground(true);
}
});
ViewCompat.setLayoutDirection(snackbar.getView(), ViewCompat.LAYOUT_DIRECTION_RTL);
snackbar.show();
}
Question.java(模型数据)
public class Question extends BaseQuestion {
private int selectedId;
private boolean showErrorBackground;
@Bindable
public void setShowErrorBackground(boolean showErrorBackground) {
this.showErrorBackground = showErrorBackground;
notifyPropertyChanged(BR.showErrorBackground);
}
@Bindable
public int getSelectedId() {
return selectedId;
}
public void setSelectedId(int selectedId) {
this.selectedId = selectedId;
notifyPropertyChanged(BR.selectedId);
}
public boolean isShowErrorBackground() {
return showErrorBackground;
}
}
谢谢你的帮助
【问题讨论】:
标签: java android radio-group