2017-08-11 44 views
0

將字符串保存到SharedPreference時出現問題。此類保存號碼卡並從SharedPreference中獲取字符串。 這是我的課MyShrePreference。我不知道爲什麼該字符串沒有保存到SharedPreference。我無法將數據保存到SharePrefence字符串

public class MySharepreference { 

    public static final String PREFS_NAME = "POSITION"; 
    public static final String POSITION = "current"; 
    public String nameString; 

    public MySharepreference() { 
     super(); 
    } 


    public void saveNumberCard(Context context, String position) { 
     SharedPreferences settings; 
     SharedPreferences.Editor editor; 
     settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
     editor = settings.edit(); 
     editor.putString(POSITION, position); 
     editor.commit(); 
    } 

    public String getNumberCard(Context context) { 
     SharedPreferences sharedPreferences; 
     sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
     if (sharedPreferences.contains(POSITION)) { 
      nameString = sharedPreferences.getString(POSITION, ""); 
     } 
     return nameString; 
    } 

} 

保存到SharedPreference到適配器

onBindViewHolder

@Override 
    public void onBindViewHolder(CardViewHolder holder, final int position) { 

    mySharepreference = new MySharepreference(); 

     if (position == lastCheckedPos) { 
      mySharepreference.saveNumberCard(mContext, card.getNumberCard()); 
} 
+0

更好地解釋你的問題。 –

+1

你可以添加代碼,你調用這個方法 –

+0

你的代碼是好的,只要刪除** if(sharedPreferences.contains(POSITION))** –

回答

0
public class SharedPreferenceUtils { 
    private static final Context context = MyApplication.getInstance(); 
    private static String SHARED_PREFERENCES_FILE_NAME = "myapplicationpref"; 
    private static SharedPreferences preference; 

    public static SharedPreferences getSharedPrefrence() { 
     if (preference == null) { 
      preference = context.getSharedPreferences(SHARED_PREFERENCES_FILE_NAME, 
        Context.MODE_PRIVATE); 
     } 
     return preference; 

    } 


    public static String getStringValueFromSharedPrefarence(String key, String defaultValue) { 
     return getSharedPrefrence().getString(key, defaultValue); 

    } 
    public static boolean getBooleanValueFromSharedPrefarence(String key, boolean defaultValue) { 
     return getSharedPrefrence().getBoolean(key, defaultValue); 
    } 

    public static boolean setBooleanValueToSharedPrefarence(String key, boolean value) { 
     return getSharedPrefrence().edit().putBoolean(key, value).commit(); 

    } 
    public static boolean setStringValueToSharedPrefarence(String key, String value) { 
     return getSharedPrefrence().edit().putString(key, value).commit(); 

    } 
    public static boolean setStringSetToSharedPrefarence(String key, Set<String> value) { 
     return getSharedPrefrence().edit().putStringSet(key, value).commit(); 

    } 

} 
+0

其餘的代碼是好的,這一行是泄漏上下文private static final Context context = MyApplication.getInstance(); ,而是可以使用單例設計模式並將應用程序上下文傳遞給它。 – NaserShaikh

0

更改代碼mContext到getApplicationContext(),像

 mySharepreference.saveNumberCard(getApplicationContext(), card.getNumberCard()); 
相關問題