2017-10-15 97 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'應該是預期的。 –