I've created programmatically 4 radio groups. The problem comes, when i try to use setOnClickListener through an inner class, that needs access to the iteration variable (i) of the loop used to declare the radio groups. I tried also to have the iterator variable final, but it didn't work. I need to set all 4 radio groups clearCheck(). Here is my code:
public class MainActivity extends AppCompatActivity {
**RadioGroup[] radioGroup;**
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = new RadioGroup[4];
for (int i = 0; i < 4; i++) {
radioGroup[i] = new RadioGroup(this);
finishButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
**radioGroup[i].clearCheck();**
}
});
linearLayout.addView(radioGroup[i]);
}
}
}
The error is: local variable i is accessed from within inner class; needs to be declared final
Thanks!