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?