I have a checkbox in android UI interface that need to set confirm. I need to declare it as final as it need to access it inside the inner class. However, I also want a public sub to check the value of the checkbox and also reset it.
The problem is I cannot access the final variable chkConfirm
inside public sub.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final CheckBox chkConfirm;
chkConfirm = (CheckBox) findViewById(R.id.chkConfirm);
chkConfirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (chkConfirm.isChecked()) {
} else {
}
}
});
chkConfirm.setChecked(false);
}
public Boolean isConfirmed() {
if (chkConfirm.isChecked() ) {
chkConfirm.setChecked(false);
return true;
} else {
return false;
}
}
How to solve?
Move the final CheckBox chkConfirm
to the class, as private variable:
private CheckBox chkConfirm;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
chkConfirm = (CheckBox) findViewById(R.id.chkConfirm);
chkConfirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (chkConfirm.isChecked()) {
} else {
}
}
});
chkConfirm.setChecked(false);
}
public Boolean isConfirmed() {
if (chkConfirm.isChecked() ) {
chkConfirm.setChecked(false);
return true;
} else {
return false;
}
}