As explained in http://tools.android.com/tips/non-constant-fields, resource ids are not final (in ADT 14). So a check with switch is broken (see: Android resource IDs suddenly not final, switch()'es broken). However after opening R.java you can see that there are final resources. So, what to use - if or switch? What versions of SDK require if statement? A Lint does not alert if I write
switch (v.getId()) {
case R.id.llBirthday:
...
break;
case R.id.llCity:
...
break;
}
The ids are converted to a unique integer value and by switching on the value of R.id.myvalue that int value will always map to that resource.
The linked question is from 2011.
As for if or switch, if you have only a couple of values then an if statement might be better, a longer list then a switch, which is programming basics. Please note this is not set in stone. When to use If-else if-else over switch statments and vice versa
We can use if else or switch both and both will work. And as you are using R.id.yourResource which will convert to an int thats why both if else and switch work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With