So, I’m trying to set up a series of checkboxes that, when clicked, will add a number, depending on the checkbox to either the int variable attackTotal or damageTotal, which will in turn be shown in some textviews.
Currently, however, clicking the top checkbox acts as if I had clicked both it, and every checkbox after it in the switch statement. The second clickbox seems to activate itself and all the following ones as well, etcetera etcetera…
So here’s the code I’ve gotten together.
public void onCheckboxClicked(View v) {
// Is the view now checked?
boolean checked = ((CheckBox) v).isChecked();
// Check which checkbox was clicked
switch(v.getId()) {
case R.id.checkBox1:
if (checked)
{
flankAttack=2;
}
else
{
flankAttack=0;
}
case R.id.checkBox2:
if (checked)
{
pbs=1;
}
else
{
pbs=0;
}
all the way down to..
case R.id.checkBox10:
if (checked)
{
attackTotal=attack+flankAttack+pbs;
damageTotal=damage+pbs;
TextView textView = (TextView) findViewById(R.id.textView2);
TextView textView2 = (TextView) findViewById(R.id.textView4);
textView.setText(Integer.toString(attackTotal));
textView2.setText(Integer.toString(damageTotal));
}
else
{
attackTotal=attack+flankAttack+pbs;
damageTotal=damage+pbs;
TextView textView = (TextView) findViewById(R.id.textView2);
TextView textView2 = (TextView) findViewById(R.id.textView4);
textView.setText(Integer.toString(attackTotal));
textView2.setText(Integer.toString(damageTotal));
}
I only started trying to figure out this programming stuff on Friday, so be gentle.
just before
case R.id.checkBox2:you need to have abreak;to tell the program to break from the switch statement. Otherwise anything meetingR.id.checkBox1just continue through and executes all the logic you have forR.id.checkBox2as well. (You needbreak;before all your other cases as well).