Android coding with switch (String)
switch
statement on String
objects is a new feature introduced in Java 1.7. Unfortunatelly Android requires version 1.6 or 1.5. This is why you have to forget for some time about such constructions.
You can avoid using if-statements-chain by storing the map of methods which will be executed for certain String
: Map<String, Method>
. You can always encapsulate Method
it with some Handler
object. Look here for more info: How to remove large if-else-if chain
and
why-cant-i-switch-on-a-string
Switch statement using a String type
switch
for Strings exists, but it's only available starting from Java 7. The syntax actually is just as with anInteger
switch:String test = "test";
switch (test) {
case "testt":
System.out.println("Wrong");
break;
case "test":
System.out.println("Got it");
break;
}There is no situation that a switch can handle, but a simple if-else could not. A switch simply is more convenient.
I'd recommend selecting lower-case starting letters for a Class' attributes.
Android switch by values from strings.xml
Unfortunately you cannot use resource string in switch-case. You have two options. Choose anyone...
use
static final String
in your activity.Example: Initialize the strings
public static final String TITLE_ACTIVITY_FIRST = "activity_title";
public static final String RADIO_BUTTON_SECCOND = "radio_button_second";
Then you can use the
TITLE_ACTIVITY_FIRST
in switch case. like,switch(text.toString()){
case TITLE_ACTIVITY_FIRST: break;
case RADIO_BUTTON_SECOND: break;
}No error will show!
use if-else. You can then use your resource strings.
Example:
if(text.toString().equals(getString(R.string.title_activity_first))){
//your code in case of 1st condition
}else if(text.toString().equals(getString(R.string.title_activity_second))){
//your code in case of 2nd condition
}
The second might look clumsy. But you wont have to change your code too much. Whereas the first one might look quite handy and you can easily modify later. Hope it helps!
Android coding with switch (String)
switch
statement on String
objects is a new feature introduced in Java 1.7. Unfortunatelly Android requires version 1.6 or 1.5. This is why you have to forget for some time about such constructions.
You can avoid using if-statements-chain by storing the map of methods which will be executed for certain String
: Map<String, Method>
. You can always encapsulate Method
it with some Handler
object. Look here for more info: How to remove large if-else-if chain
and
why-cant-i-switch-on-a-string
Switch statement of string
Java does not support String in switch/case earlier java 7. But if you use java 6 you can achieve the desired result by using an enum.
private enum Fruit {
apple, carrot, mango, orange;
}
String value; // assume input
Fruit fruit = Fruit.valueOf(value); // surround with try/catch
switch(fruit) {
case apple:
method1;
break;
case carrot:
method2;
break;
// etc...
}
Related Topics
Android Simpledateformat, How to Use It
How to Draw Circle by Canvas in Android
Does Glide Have a Method for Loading Both Png and Svg
How to Delete a File on Google Drive Using Google Drive Android API
How to Skip Initial Data and Trigger Only New Updates in Firestore Firebase
How to Set Up Intellij Idea for Android Applications
How to Use Weakreference in Java and Android Development
How to Avoid Garbage Collection Delays in Java Games? (Best Practices)
How to Parse JSON from a Java Httpresponse
Android Web Scraping with a Headless Browser
How to Get Current Location in Googlemap Using Fusedlocationproviderclient
Android: Textview Automatically Truncate and Replace Last 3 Char of String
How to Hash a String in Android
Change Icons of Checked and Unchecked for Checkbox for Android
Java Lang Unsupportedclassversion Error in Xamarin Studio