我正在尝试访问if语句中的cb
,但我收到cb cant be resolved
我尝试将Checkbox cb
声明为类变量,但我得到了The method getId() in the type View is not applicable for the arguments (int)
。
我尝试将其声明为方法局部变量,例如final CheckBox cb;
,但我收到两个错误:第一行The final local variable cb may already have been assigned
位于此行cb = new CheckBox(this);
而第二行The method getId() in the type View is not applicable for the arguments (int)
我该如何解决?
private void createCheckboxList(final ArrayList<Integer> items) {
//final CheckBox cb;
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(String.valueOf(items.get(i)));
cb.setId(i);
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i : items) {
if (cb.getId(i).isChecked()) {
}
}
}
});
}
答案 0 :(得分:1)
参考cb不会出现在&#39;之外。环。既然你知道它的id就可以创建一个新的复选框引用并使用findviewById();引用相同的复选框
cb.getId()返回并且整数不是复选框引用
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
for (int i = 0; i < items.size(); i++) {
final CheckBox cb = new CheckBox(this);
cb.setText(String.valueOf(items.get(i)));
cb.setId(i);
ll.addView(cb);
}
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
btn.setText("submit");
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < items.size()) { \\
Checkbox ch=(Checkbox) findViewById(i); \\
if (ch.isChecked()) {
}
}
}
});
}