2017-10-15 93 views
-2

我想使用SharedPreferences中给出了答案hereSharedPreferences阅读诠释

我想从中读取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(); 
    } 

} 

为什么?

+0

请提供一个完整** **代码例子的字符串。这个行在'onCreate()'方法中?如果是这样,请在代码**中显示**。同时显示封闭类。请注意不要包含与您的问题无关的额外代码。查看[mcve]了解更多有关创建良好代码示例的技巧。 –

+1

共享偏好或只是一个偏好! – Xenolion

+0

我想问题是你正在尝试读取一个整数值,但你设置默认值为“”字符串,而不是一个整数。您应该分享readInteger()方法的确切答案。 – Thracian

回答

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:如果您提供课程,我们可以提供更好的帮助。

+0

什么是KEY_VALUE,DEFAULT_VALUE在这里? – Priya

+0

KEY_VALUE是您用于保存值的字符串,例如“MY_INTERGER”,而缺省值是您在首选项中不存在key_value时获得的值 –

+0

我将为您编辑答案。 –

0

的问题是,readInteger()没有最后的说法是Stringint所以请改变这一行的静态方法也themeNew也应该Stringkey您用来检索您的Integer主题:

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

是这样的:

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

哪里0是默认值,如果你喜欢未找到!快乐编码!

+0

它给我错误称为错误的第二个参数类型。找到int required java.lang.String – Priya

+1

是的,第二个参数应该是Key的字符串名称。我已经更新了答案来修改它! @Priya – Xenolion

0

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

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

这种方法需要利用DefValue是一个INTPreferences.readInteger(getApplicationContext(), Preferences.themeNew,"");“”不是int值,它是具有零长度

+0

请注意,OP还会在第二个参数中传递一个'int',其中'String'应该是预期的。 –