Java code critique request [closed]
- by davidk01
Can you make sense of the following bit of java code and do you have any suggestions for improving it? Instead of writing four almost identical setOnClickListener method calls I opted to iterate over an array but I'm wondering if this was the best way to do it. Here's the code:
/* Set up the radio button click listeners so two categories are not selected
at the same time. When one of them is clicked it clears the others.
*/
final RadioButton[] buttons = {radio_books,radio_games,radio_dvds,radio_electronics};
for (int i = 0; i < 4; i++) {
final int k = i;
buttons[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int j = 0; j < 4; j++) {
if (buttons[j] != buttons[k]) {
buttons[j].setChecked(false);
}
}
}
});
}