I’m creating an android app which will call a method which parses the selected item of multiple spinner objects. I’m very new to Java, and I’m not sure which statement is best to use in terms of program flow in this instance. Should I just use multiple If statements? Such as:
If(spinner1.getSelectedItemPosition() == 0 && spinner2.getSelectedItemPosition() == 2 && spinner3.getSelectedItemPosition() == 4)
/* do some stuff
Or would it be better to use switch statements?
switch(spinner1.getSelectedItem())
case 1:
switch(spinner2.getSelectedItem())
case 1:
switch(spinner3.getSelectedItem())
case 1:
/* do something
Essentially what I’m trying to do here is evaluate the selected item of each spinner object, then create a new Activity based on that evaluation. However the initial activity consists of several spinner boxes, each with several options, so I’m not sure the best way to go about designing the control statements. Using an if statement for every single possible combination of selected items seems a bit unwieldy. Additionally, I’m not sure which option will actually make the code work the way I intend it to.
Probably a switch statement will be more effective, because you can send multiple results into the same section of code (sounds like that might be necessary for your application) like this
If value evaluates to either 0 or 1, the first section will be executed and you only had to write the operation code once. As for specifics (now I’m just getting creative), you could shift the three spinner values into a single integer and switch on the result. If you write the case statements in hexadecimal, it will be more readable as to the state of each.
Hope that Helps!
Possible drawback to this example: Only 256 options allowed for each spinner 😉