I have a multiplechoice dialog on my activity. I have a boolean array to keep track of which items are selected from the dialog. So I want each time an item is clicked to make the corresponding place on my array true. The array is being created in the onCreateDialog before the return.
public Dialog onCreateDialog(int id) {
userlistlocal=getuserlist(username);
boolean[] usernamechecked=new boolean[userlistlocal.length/3];
Arrays.fill(usernamechecked,false);
return new AlertDialog.Builder(Newevent.this)
.setIcon(R.drawable.ic_menu_invite)
.setTitle("Add People")
.setMultiChoiceItems(userselectionlistlocal,
null,new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,
boolean isChecked) {
/* User clicked on a check box do some stuff */
usernamechecked[whichButton]=isChecked;<-------HERE
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Yes so do some stuff */
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked No so do some stuff */
}
})
.create();
}
But when I put in the onClick method my boolean array it says it has to be final(Cannot refer to a non-final variable usernamechecked inside an inner class defined in a different method). I dont know what to do
You cannot access your array if it is not declared
final. But if it is declaredfinalyou have to consider that you can’t modify it any more.A method to resolve this problem is to define the
OnMultiChoiceClickListenernot as an anonymous inner class. For example you could define it as a inner class in the same class or even as a separate class.