Reputation: 2123
Basically I got 5 rows of 5 checkboxes, this is just one row. As you can notice it's messy and having 5 of these is even worse.
I just started learning android so I'm not yet familiar with the options I got, but normally I think I'd either put all the boxes in an array and iterate through it, or make a JPanel for one row and add the panel 5 times.
What's the best way to do this and why?
/**
* Row 1 checkboxes
*/
CheckBox row1box1 = (CheckBox) findViewById(R.id.row1checkBox1);
CheckBox row1box2 = (CheckBox) findViewById(R.id.row1checkBox2);
CheckBox row1box3 = (CheckBox) findViewById(R.id.row1checkBox3);
CheckBox row1box4 = (CheckBox) findViewById(R.id.row1checkBox4);
CheckBox row1box5 = (CheckBox) findViewById(R.id.row1checkBox5);
row1box1.setOnClickListener(new StartListener(row1box1, chronometer, ringtone));
row1box2.setOnClickListener(new StartListener(row1box2, chronometer, ringtone));
row1box3.setOnClickListener(new StartListener(row1box3, chronometer, ringtone));
row1box4.setOnClickListener(new StartListener(row1box4, chronometer, ringtone));
row1box5.setOnClickListener(new StartListener(row1box5, chronometer, ringtone));
Upvotes: 1
Views: 108
Reputation: 8231
I suppose you could try something along these lines. Completely untested:
private int[] checkBoxIds = new int[] { R.id.row1checkBox1, R.id.row1checkBox 2...(etc.)}
private List<CheckBox> checkBoxList= new ArrayList<>(checkBoxIds.length);
for (int i = 0; i < checkBoxIds.length; i++) {
checkBoxList.add(i, (CheckBox) findViewById(checkBoxIds[i]));
checkBoxList.get(i).setOnClickListener(new StartListener(checkBoxList.get(i), chronometer, ringtone);
}
Upvotes: 0
Reputation: 13321
One solution would be to use the View Lists option from ButterKnife.
@Bind({ R.id.row1checkBox1, R.id.row1checkBox2, R.id.row1checkBox3})
List<CheckBox> checkBoxViews;
Then you can use ButterKnife.apply
to act on all the views at once, or you can iterate the list.
Upvotes: 1