2017-08-11 71 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());