我将字符串保存到SharedPreference时遇到问题。此类保存号码卡并从SharedPreference获取字符串。 这是我的班级MyShrePreference。我不知道为什么该字符串没有保存到SharedPreference。
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());
}