Android Coding with Switch (String)

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

  1. switch for Strings exists, but it's only available starting from Java 7. The syntax actually is just as with an Integer switch:

    String test = "test";
    switch (test) {
    case "testt":
    System.out.println("Wrong");
    break;
    case "test":
    System.out.println("Got it");
    break;
    }
  2. There is no situation that a switch can handle, but a simple if-else could not. A switch simply is more convenient.

  3. 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...

  1. 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!

  2. 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



Leave a reply



Submit