將字符串保存到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());
}
更好地解釋你的問題。 –
你可以添加代碼,你調用這個方法 –
你的代碼是好的,只要刪除** if(sharedPreferences.contains(POSITION))** –