Android - 如果值为空则隐藏复选框
Android - Hiding CheckBoxes if value is empty
我正在寻找一种更好的方法来隐藏我的复选框。每种情况下至少有 2 个和最多 5 个复选框(5 个答案复选框和 5 个解决方案复选框)。
我正在用我的数据库的值动态填充答案 Arraylist。然后我检查每个案例列表是否有值。如果有值,则 CheckBox 获取答案文本,否则文本为空。之后我想隐藏所有具有空值的答案复选框。此外,我还必须处理相关的解决方案 CheckBoxes。这是我的实现方式:
if (question.getQuestiontype() == 2) {
cb_answer1.setText(answerList.size() >= 1 ? answerList.get(0).getAnswertext() : null);
cb_answer2.setText(answerList.size() >= 2 ? answerList.get(1).getAnswertext() : null);
cb_answer3.setText(answerList.size() >= 3 ? answerList.get(2).getAnswertext() : null);
cb_answer4.setText(answerList.size() >= 4 ? answerList.get(3).getAnswertext() : null);
cb_answer5.setText(answerList.size() >= 5 ? answerList.get(4).getAnswertext() : null);
if (cb_answer3.getText().toString().isEmpty()) {
cb_answer3.setVisibility(View.INVISIBLE);
cb_answer4.setVisibility(View.INVISIBLE);
cb_answer5.setVisibility(View.INVISIBLE);
cb_solution3.setVisibility(View.INVISIBLE);
cb_solution4.setVisibility(View.INVISIBLE);
cb_solution5.setVisibility(View.INVISIBLE);
} else {
cb_answer3.setVisibility(View.VISIBLE);
cb_answer4.setVisibility(View.VISIBLE);
cb_answer5.setVisibility(View.VISIBLE);
cb_solution3.setVisibility(View.VISIBLE);
cb_solution4.setVisibility(View.VISIBLE);
cb_solution5.setVisibility(View.VISIBLE);
}
if (cb_answer4.getText().toString().isEmpty()) {
cb_answer4.setVisibility(View.INVISIBLE);
cb_answer5.setVisibility(View.INVISIBLE);
cb_solution4.setVisibility(View.INVISIBLE);
cb_solution5.setVisibility(View.INVISIBLE);
} else {
cb_answer4.setVisibility(View.VISIBLE);
cb_answer5.setVisibility(View.VISIBLE);
cb_solution4.setVisibility(View.VISIBLE);
cb_solution5.setVisibility(View.VISIBLE);
}
if (cb_answer5.getText().toString().isEmpty()) {
cb_answer5.setVisibility(View.INVISIBLE);
cb_solution5.setVisibility(View.INVISIBLE);
} else {
cb_answer5.setVisibility(View.VISIBLE);
cb_solution5.setVisibility(View.VISIBLE);
}
for (CheckBox cb : answerCheckBoxes) {
cb.setEnabled(true);
}
}
看起来有点尴尬,我认为这不是处理这个问题的最佳方式。
你能给我一些建议吗?
视图数组怎么样?
示例:
CheckBox[] answers = new CheckBox[] {cb_answer1, cb_answer2, cb_answer3, cb_answer4, cb_answer5};
for (int i = answerList.size(); i < answers.length; i++) {
answers[i].setVisibility(View.INVISIBLE);
}
请随意扩展!
首先,如果我的理解很好,我认为你可以像这里一样简化你的代码分离 cbs 逻辑:
if(question.getQuestiontype()==2)
{
cb_answer1.setText(answerList.size() >= 1 ? answerList.get(0).getAnswertext() : null);
cb_answer2.setText(answerList.size() >= 2 ? answerList.get(1).getAnswertext() : null);
cb_answer3.setText(answerList.size() >= 3 ? answerList.get(2).getAnswertext() : null);
cb_answer4.setText(answerList.size() >= 4 ? answerList.get(3).getAnswertext() : null);
cb_answer5.setText(answerList.size() >= 5 ? answerList.get(4).getAnswertext() : null);
if (cb_answer3.getText().toString().isEmpty()) {
cb_answer3.setVisibility(View.INVISIBLE);
cb_solution3.setVisibility(View.INVISIBLE);
} else {
cb_answer3.setVisibility(View.VISIBLE);
cb_solution3.setVisibility(View.VISIBLE);
}
if (cb_answer4.getText().toString().isEmpty()) {
cb_answer4.setVisibility(View.INVISIBLE);
cb_solution4.setVisibility(View.INVISIBLE);
} else {
cb_answer4.setVisibility(View.VISIBLE);
cb_solution4.setVisibility(View.VISIBLE);
}
if (cb_answer5.getText().toString().isEmpty()) {
cb_answer5.setVisibility(View.INVISIBLE);
cb_solution5.setVisibility(View.INVISIBLE);
} else {
cb_answer5.setVisibility(View.VISIBLE);
cb_solution5.setVisibility(View.VISIBLE);
}
for (CheckBox cb : answerCheckBoxes) {
cb.setEnabled(true);
}
}
然后查看这段代码,您可以看到您正在对各种答案和响应 cbs 执行相同的操作。所以你可以创建一个像这样的参数化方法:
if(question.getQuestiontype()==2) {
cb_answer1.setText(answerList.size() >= 1 ? answerList.get(0).getAnswertext() : null);
cb_answer2.setText(answerList.size() >= 2 ? answerList.get(1).getAnswertext() : null);
cb_answer3.setText(answerList.size() >= 3 ? answerList.get(2).getAnswertext() : null);
cb_answer4.setText(answerList.size() >= 4 ? answerList.get(3).getAnswertext() : null);
cb_answer5.setText(answerList.size() >= 5 ? answerList.get(4).getAnswertext() : null);
this.setCheckboxVisibility(cb_answer3, cb_solution3);
this.setCheckboxVisibility(cb_answer4, cb_solution4);
this.setCheckboxVisibility(cb_answer5, cb_solution5);
for (CheckBox cb : answerCheckBoxes) {
cb.setEnabled(true);
}
}
private void setCheckboxVisibility(Checkbox answer, Checkbox solution) {
if (answer.getText().toString().isEmpty()) {
answer.setVisibility(View.INVISIBLE);
solution.setVisibility(View.INVISIBLE);
} else {
answer.setVisibility(View.VISIBLE);
solution.setVisibility(View.VISIBLE);
}
}
然后您可以将此解决方案与 TWL
发布的解决方案结合使用
我正在寻找一种更好的方法来隐藏我的复选框。每种情况下至少有 2 个和最多 5 个复选框(5 个答案复选框和 5 个解决方案复选框)。
我正在用我的数据库的值动态填充答案 Arraylist。然后我检查每个案例列表是否有值。如果有值,则 CheckBox 获取答案文本,否则文本为空。之后我想隐藏所有具有空值的答案复选框。此外,我还必须处理相关的解决方案 CheckBoxes。这是我的实现方式:
if (question.getQuestiontype() == 2) {
cb_answer1.setText(answerList.size() >= 1 ? answerList.get(0).getAnswertext() : null);
cb_answer2.setText(answerList.size() >= 2 ? answerList.get(1).getAnswertext() : null);
cb_answer3.setText(answerList.size() >= 3 ? answerList.get(2).getAnswertext() : null);
cb_answer4.setText(answerList.size() >= 4 ? answerList.get(3).getAnswertext() : null);
cb_answer5.setText(answerList.size() >= 5 ? answerList.get(4).getAnswertext() : null);
if (cb_answer3.getText().toString().isEmpty()) {
cb_answer3.setVisibility(View.INVISIBLE);
cb_answer4.setVisibility(View.INVISIBLE);
cb_answer5.setVisibility(View.INVISIBLE);
cb_solution3.setVisibility(View.INVISIBLE);
cb_solution4.setVisibility(View.INVISIBLE);
cb_solution5.setVisibility(View.INVISIBLE);
} else {
cb_answer3.setVisibility(View.VISIBLE);
cb_answer4.setVisibility(View.VISIBLE);
cb_answer5.setVisibility(View.VISIBLE);
cb_solution3.setVisibility(View.VISIBLE);
cb_solution4.setVisibility(View.VISIBLE);
cb_solution5.setVisibility(View.VISIBLE);
}
if (cb_answer4.getText().toString().isEmpty()) {
cb_answer4.setVisibility(View.INVISIBLE);
cb_answer5.setVisibility(View.INVISIBLE);
cb_solution4.setVisibility(View.INVISIBLE);
cb_solution5.setVisibility(View.INVISIBLE);
} else {
cb_answer4.setVisibility(View.VISIBLE);
cb_answer5.setVisibility(View.VISIBLE);
cb_solution4.setVisibility(View.VISIBLE);
cb_solution5.setVisibility(View.VISIBLE);
}
if (cb_answer5.getText().toString().isEmpty()) {
cb_answer5.setVisibility(View.INVISIBLE);
cb_solution5.setVisibility(View.INVISIBLE);
} else {
cb_answer5.setVisibility(View.VISIBLE);
cb_solution5.setVisibility(View.VISIBLE);
}
for (CheckBox cb : answerCheckBoxes) {
cb.setEnabled(true);
}
}
看起来有点尴尬,我认为这不是处理这个问题的最佳方式。 你能给我一些建议吗?
视图数组怎么样?
示例:
CheckBox[] answers = new CheckBox[] {cb_answer1, cb_answer2, cb_answer3, cb_answer4, cb_answer5};
for (int i = answerList.size(); i < answers.length; i++) {
answers[i].setVisibility(View.INVISIBLE);
}
请随意扩展!
首先,如果我的理解很好,我认为你可以像这里一样简化你的代码分离 cbs 逻辑:
if(question.getQuestiontype()==2)
{
cb_answer1.setText(answerList.size() >= 1 ? answerList.get(0).getAnswertext() : null);
cb_answer2.setText(answerList.size() >= 2 ? answerList.get(1).getAnswertext() : null);
cb_answer3.setText(answerList.size() >= 3 ? answerList.get(2).getAnswertext() : null);
cb_answer4.setText(answerList.size() >= 4 ? answerList.get(3).getAnswertext() : null);
cb_answer5.setText(answerList.size() >= 5 ? answerList.get(4).getAnswertext() : null);
if (cb_answer3.getText().toString().isEmpty()) {
cb_answer3.setVisibility(View.INVISIBLE);
cb_solution3.setVisibility(View.INVISIBLE);
} else {
cb_answer3.setVisibility(View.VISIBLE);
cb_solution3.setVisibility(View.VISIBLE);
}
if (cb_answer4.getText().toString().isEmpty()) {
cb_answer4.setVisibility(View.INVISIBLE);
cb_solution4.setVisibility(View.INVISIBLE);
} else {
cb_answer4.setVisibility(View.VISIBLE);
cb_solution4.setVisibility(View.VISIBLE);
}
if (cb_answer5.getText().toString().isEmpty()) {
cb_answer5.setVisibility(View.INVISIBLE);
cb_solution5.setVisibility(View.INVISIBLE);
} else {
cb_answer5.setVisibility(View.VISIBLE);
cb_solution5.setVisibility(View.VISIBLE);
}
for (CheckBox cb : answerCheckBoxes) {
cb.setEnabled(true);
}
}
然后查看这段代码,您可以看到您正在对各种答案和响应 cbs 执行相同的操作。所以你可以创建一个像这样的参数化方法:
if(question.getQuestiontype()==2) {
cb_answer1.setText(answerList.size() >= 1 ? answerList.get(0).getAnswertext() : null);
cb_answer2.setText(answerList.size() >= 2 ? answerList.get(1).getAnswertext() : null);
cb_answer3.setText(answerList.size() >= 3 ? answerList.get(2).getAnswertext() : null);
cb_answer4.setText(answerList.size() >= 4 ? answerList.get(3).getAnswertext() : null);
cb_answer5.setText(answerList.size() >= 5 ? answerList.get(4).getAnswertext() : null);
this.setCheckboxVisibility(cb_answer3, cb_solution3);
this.setCheckboxVisibility(cb_answer4, cb_solution4);
this.setCheckboxVisibility(cb_answer5, cb_solution5);
for (CheckBox cb : answerCheckBoxes) {
cb.setEnabled(true);
}
}
private void setCheckboxVisibility(Checkbox answer, Checkbox solution) {
if (answer.getText().toString().isEmpty()) {
answer.setVisibility(View.INVISIBLE);
solution.setVisibility(View.INVISIBLE);
} else {
answer.setVisibility(View.VISIBLE);
solution.setVisibility(View.VISIBLE);
}
}
然后您可以将此解决方案与 TWL
发布的解决方案结合使用