String comparison via the Java switch statement


I need to compare the value that is in the object with each value from the prepared array. How do I do this? In my case, it gives some incomprehensible error

    String text = textView.getText().toString();
    switch (text) {
        case labelsStr[0]:
            startActivity(new Intent(context, Color.class));
            break;
    }
Author: Mii, 2020-07-27

1 answers

Since in java switch by line added in version 7, then in android switch by line works starting with API 19 KitKat (in this version, actually added support for java 7).

To be more precise, it will run on earlier versions (18 for sure), but you need to compile from at least sdk version 19 and jdk 7.

Here is the required configuration:

android {
  compileSdkVersion 19
  buildToolsVersion "19.0.0"

  defaultConfig {
    minSdkVersion 7
    targetSdkVersion 19
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
  }
}
 1
Author: Roman Konoval, 2020-07-27 18:10:17