SharedPreferences读取int

时间:2017-10-15 20:50:58

标签: java android sharedpreferences

我正在尝试使用SharedPreferences,就像给出here

的答案一样

我正在尝试从下面的代码中读取int值

int theme= Preferences.readInteger(getApplicationContext(), Preferences.themeNew,"");

但它给我的错误如下

readInteger() in Preferences cannot be applied to:
Expected : Actual
Parameter : Arguments 

偏好等级

public class Preferences {
    public static final String PREF_NAME = "your preferences name";

    public static final int MODE = Context.MODE_PRIVATE;

    public static final int themeNew = 1;
    public static final String USER_NAME = "USER_NAME";

    public static final String NAME = "NAME";
    public static final String EMAIL = "EMAIL";
    public static final String PHONE = "PHONE";
    public static final String address = "address";

    public static void writeBoolean(Context context, String key, boolean value) {
        getEditor(context).putBoolean(key, value).commit();
    }

    public static boolean readBoolean(Context context, String key,
                                      boolean defValue) {
        return getPreferences(context).getBoolean(key, defValue);
    }

    public static void writeInteger(Context context, String key, int value) {
        getEditor(context).putInt(key, value).commit();

    }

    public static int readInteger(Context context, String key, int defValue) {
        return getPreferences(context).getInt(key, defValue);
    }

    public static void writeString(Context context, String key, String value) {
        getEditor(context).putString(key, value).commit();

    }

    public static String readString(Context context, String key, String defValue) {
        return getPreferences(context).getString(key, defValue);
    }

    public static void writeFloat(Context context, String key, float value) {
        getEditor(context).putFloat(key, value).commit();
    }

    public static float readFloat(Context context, String key, float defValue) {
        return getPreferences(context).getFloat(key, defValue);
    }

    public static void writeLong(Context context, String key, long value) {
        getEditor(context).putLong(key, value).commit();
    }

    public static long readLong(Context context, String key, long defValue) {
        return getPreferences(context).getLong(key, defValue);
    }

    public static SharedPreferences getPreferences(Context context) {
        return context.getSharedPreferences(PREF_NAME, MODE);
    }

    public static SharedPreferences.Editor getEditor(Context context) {
        return getPreferences(context).edit();
    }

}

为什么?

3 个答案:

答案 0 :(得分:0)

您应该使用:

String KEY_VALUE = "MY_KEY";
int not_found = -1;
int theme = getPreferences(MODE_PRIVATE).getInt(KEY_VALUE,not_found);

其中MODE_PRIVATE表示您正在使用的操作模式(MODE_PRIVATE是默认模式)。

在你的情况下,not_found应该是default_theme。

我认为您的代码不起作用,因为您在预期使用int时会发送一个字符串值(预期int值为your_default值,但您正在使用字符串)。您的代码是否成功构建?

ps:如果你提供课程,我们可以提供更好的帮助。

答案 1 :(得分:0)

问题是静态方法readInteger()的最后一个参数不是String而是int所以请更改此行themeNew也应该是String您用来检索key主题的1}} Integer

    int theme= Preferences.readInteger(getApplicationContext(), Preferences.themeNew,"");

要成为这样:

    int theme= Preferences.readInteger(getApplicationContext(), "my_theme",0);

如果找不到您的偏好,0是默认值!快乐的编码!

答案 2 :(得分:0)

您的问题是为int值设置字符串值。

 public static int readInteger(Context context, String key, int defValue) {
        return getPreferences(context).getInt(key, defValue);
 }

此方法要求defValue为 int ,但Preferences.readInteger(getApplicationContext(), Preferences.themeNew,"");,“”不是int值,它是长度为零的String