Does switch/case not work in Android apps?
The following seems to work just fine in Android:
var sortOrder = 0;
if (sortOrder == 0){
alert('Success!');
}
but THIS does not (even though it works fine on iPhone):
var sortOrder = 0;
switch (sortOrder){
case 0:
alert('Success!');
break;
}
Can anyone explain why? Do I just have to use if…else instead of switch/case on Android?
8 Answers
-
Accepted Answer
Remember that '==' in javascript will automatically typecast variables to be of the same type. My guess is you have "0" as a string rather than a number.
You can run the following code in rhino to see this for yourself:
var a = "0"; if (a == 0) { // is true } if (a === 0) { // is false } switch (a) { case 0: // won't match break; default: // will }
-
Try using Ti.API.info instead of alert and see if you get output. If so, then it's possible that your alert is coming up behind your window.
-
This is just something REALLY stupid and silly to try…. but I'd just take a shot at adding the "default" case… maybe Androids compiler is being way more strict than it needs to be
if that doesn't work, sorry for wasting your time :-/
-
Thanks Don. Actually, in my code, I was using Ti.API.info and it doesn't work. I only switched it to alert in my sample above to make it a little clearer.
-
oh yeah… totally overlooked the fact you were using "alert" like Don saw… definitely not the debugging method of choice in this environment
-
@Kenn,
@dasher did a quick test and it worked for him on Android. That's why we're postulating that it might actually be a different issue. Could you share more of the code and/or a put a Trace level log in a pastie and share it with us?
-
Don, here's my actual code:
search.addEventListener('return', function(e) { Ti.API.info('search.addEventListener called'); search.blur(); currentWord = e.value; Ti.API.info('currentWord = ' + currentWord); if (currentWord != ''){ Ti.API.info('sortOrder = ' + sortOrder); switch (sortOrder){ case 0: Ti.API.info('Calling searchRhymes'); message = searchRhymes(currentWord); break; case 1: message = searchRhymes(currentWord); break; case 2: message = searchSoundsLike(currentWord); break; case 3: message = searchContains(currentWord, 1); break; case 4: message = searchContains(currentWord, 2); break; case 5: message = searchContains(currentWord, 3); break; } } webview.html = htmlstart + message + htmlend; });
When the app runs, here's the log:
[INFO] Launching Android emulator...one moment [INFO] Building RhymeNow for Android ... one moment [INFO] Waiting for the Android Emulator to become available [INFO] Copying project resources.. [INFO] Tiapp.xml unchanged, skipping class generation [INFO] Manifest unchanged, skipping Java build [INFO] Re-launching application ... RhymeNow [INFO] Launching application ... RhymeNow [INFO] [7,7899] search.addEventListener called [INFO] [42,7941] currentWord = test [INFO] [1,7942] sortOrder = 0
Here's a Trace level log
As you can see, even though sortOrder == 0, the case 0: never executes. However, if I replace the switch/case statement with the following, it works just fine:
if (sortOrder == 0){ message = searchRhymes(currentWord); } else if (sortOrder == 1){ message = searchRhymes(currentWord); } else if (sortOrder == 2){ message = searchContains(currentWord, 1); } else if (sortOrder == 3){ message = searchContains(currentWord, 1); } else if (sortOrder == 4){ message = searchContains(currentWord, 2); } else if (sortOrder == 5){ message = searchContains(currentWord, 3); }
There are other switch/case statements in the app as well, and none of them execute.
-
Damien, that was it! I changed my switch statement to
switch (parseInt(sortOrder)){
and that solved the problem.
What's troubling about this is that the switch statement worked well on iPhone, but broke on Android.
Thanks for your help!