0

Previously, I was saving the keys inside SharedPreferencesManager class using this way

public static final int KEY_COUNTRY_ID = 5,
     KEY_COUNTRY_NAME = 10,
     KEY_CURRENCY_ID = 15,
     KEY_CURRENCY_NAME = 20;

I decided to use an enum to store the keys because I think it is a better way than the old way, Here is what I tried.

SharedPreferencesManager.java

public class SharedPreferencesManager {

    private static SharedPreferences sharedPreferences;
    private static SharedPreferences.Editor editor;

    private static void initializeSharedPreferences(Context context) {
        if (sharedPreferences == null)
            sharedPreferences = context.getSharedPreferences("SharedPreferencesManager", Context.MODE_PRIVATE);
    }

    private static void initializeEditor() {
        if (editor == null)
            editor = sharedPreferences.edit();
    }

    public static void setValue(Context context, String key, int value) {
        initializeSharedPreferences(context);
        initializeEditor();
        editor.putInt(key, value);
        editor.apply();
    }

    public static int getIntegerValue(Context context, String key) {
        initializeSharedPreferences(context);
        switch (key) {
            case Keys.COUNTRY_ID.name():
            case Keys.CURRENCY_ID.name():
                return sharedPreferences.getInt(key, 5);
        }
        return -1;
    }

    public enum Keys {
        COUNTRY_ID,
        COUNTRY_NAME,
        CURRENCY_ID,
        CURRENCY_NAME
    }

}

The error

Constant expression required

The red lines under case Keys.COUNTRY_ID.name(): and case Keys.CURRENCY_ID.name():

How can I solve the problem?

Taha Sami
  • 1,565
  • 1
  • 16
  • 43
  • 2
    You would need to switch on an instance of `Keys` rather than a string. – khelwood Jul 06 '22 at 14:38
  • 1
    As the compiler tells you, you need to use a constant String expression if you use `switch(aStringVariable)`. The correct way to do this is to convert the `String` value to an enum constant and then use `switch` on that. – f1sh Jul 06 '22 at 14:39

1 Answers1

1

As the compiler tells you, you need to use a constant String expression if you use switch(aStringVariable). The correct way to do this is to convert the String value to an enum constant and then use switch on that.

Convert the String to an enum instant using

Keys keyConstant = Keys.valueOf(key); //argument of "COUNTRY_ID" returns Keys.COUNTRY_ID

This looks up an enum constant by that name (matching the name()). Then use it in the switch:

switch(keyConstant) {
  case COUNTRY_ID: ...
}
f1sh
  • 11,489
  • 3
  • 25
  • 51
  • It's working, Thank you. Do you think I took the right decision by leaving the old method and using the new method? – Taha Sami Jul 06 '22 at 14:47
  • 1
    @luq891 yes. From what I see, you were using the `public static final` fields as an enum. You can even assign numeric values to an enum constant in case you need to keep that functionality. – f1sh Jul 06 '22 at 14:50