Note: This is not a duplicate of Java switch statement: Constant expression required, but it IS constant because the solution to that link was already applied here.
In a Cordova App with typescript I use this enum to send my actions =
typescript
enum PluginActions {
PICK = "PICK",
PICK_MULTIPLE = "PICK_MULTIPLE"
}
I send that to cordova and in Java I get that as an action
string variable in my method.
@Override
public boolean execute(String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException {
}
There I also have an enum.
Java
enum PickerActions {
PICK, PICK_MULTIPLE
}
I want to compare the typescript PluginActions
vs the java PickerActions
.
Using if
I can do it using:
if (action.equals(PickerActions.PICK.name())) { }
but I wan to do it with a switch so I can easily add more actions
switch (action) {
case PickerActions.PICK.name():
JSONObject filters = inputs.optJSONObject(0);
this.chooseFile(filters, callbackContext);
return true;
default:
Log.w(this.LOGGER_TAG, "No function was found for " + action);
return false;
}
But I get an error there: error: constant string expression required
Is there a way to use an enum string name in a switch statement?
Edit:
As per @Arnaud recommendation I casted the value of the enum in this way:
final PickerActions pickerAction = FilePickerActions.valueOf(action);
switch (pickerAction ) {
case PickerActions.PICK:
JSONObject filters = inputs.optJSONObject(0);
this.chooseFile(filters, callbackContext);
return true;
default:
Log.w(this.LOGGER_TAG, "No function was found for " + action);
return false;
}
But I get another error there regarding case PickerAction.Pick
error: an enum switch case label must be the unqualified name of an enumeration constant