我不是sharedPreferences
的新手,我决定用它们来存储用户创建的课程标题,然后加载到listView中。课程标题存储在一个集合中,然后放入sharedPreference
。这里的问题是当我输入第一个课程(作为用户)时,它会被持久保存,但添加更多课程也会保存,并且在应用程序处于活动状态时有效,但在应用程序重新启动时,sharedPreference会返回到初始状态集合中只有一个项目的值。
当然,我查找了这个问题,并且能够使用editor.clear()
来解决这个问题,我从这个参考文献中https://looksok.wordpress.com/2012/09/21/sharedpreferences-not-saved-after-app-restart/
请注意,解决方案被描述为"棘手的问题"。我真的想知道这条线的含义。我已经检查了这个问题,但仍然无法理解为什么这个案例如此不同,每当我使用这些课程时,这种异常都会让我感到害怕。所以这是我的代码:
// Shared Preferences
SharedPreferences customPrefs;
// Editor for Shared preferences
SharedPreferences.Editor editor;
private CustomCourseDBHelper customDBHelper;
private final static int ADD_CUSTOM_COURSE_REQUEST = 1;
private final static int EDIT_CUSTOM_COURSE_REQUEST = 2;
private final static String TITLE_PREFS = "customCourseTitles";
private final static String TITLE_PREF_LIST = "customCourseList";
private ListView cListView;
private Set<String> allTitles = new LinkedHashSet<>();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_course_list);
mToolbar = (Toolbar) findViewById(R.id.tool_bar_shadow);
cListView = (ListView) findViewById(R.id.customQuestionList);
addButton = (FloatingActionButton) findViewById(R.id.addButton);
cAdapter = new CustomCourseAdapter(this);
setSupportActionBar(mToolbar);
cListView.setAdapter(cAdapter);
//declare shared prefs
customPrefs = getSharedPreferences(TITLE_PREFS, MODE_PRIVATE);
//set on long press options
optionsList.add("Delete Course");
//set icon for add button
addButton.setImageDrawable(getResources()
.getDrawable(R.drawable.ic_add_white_24dp));
addButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(CustomCourseList.this, AddCustomCourseActivity.class);
startActivityForResult(i, ADD_CUSTOM_COURSE_REQUEST);
}
});
}
在这里发生了豁免
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK && requestCode == ADD_CUSTOM_COURSE_REQUEST){
CustomCourseItem cci = new CustomCourseItem(data);
cAdapter.add(cci);
//save the provided details in a database
customDBHelper = new CustomCourseDBHelper(this, cci.getPath());
customDBHelper.addCustomCourse(cci);
editor = customPrefs.edit();
Set<String> mSet = customPrefs.getStringSet(TITLE_PREF_LIST, null);
if (mSet != null)
allTitles = mSet;
//store the given database names in a set in a shared preference
allTitles.add(cci.getPath());
editor.clear(); //this line here, without it, the sharedPref is not persistent,
//i didn't see any clear reason as to why this occurs, when i read the documentary
editor.putStringSet(TITLE_PREF_LIST, allTitles);
editor.commit();
}
//listen request for edits made to courses
if (resultCode == RESULT_OK && requestCode == EDIT_CUSTOM_COURSE_REQUEST){
if (data.getBooleanExtra("edited", false)){
restructureDbOnEdit();
}
}
}
PS。我省略了一些不必要的代码,如UI实现和其他非sharedPreference相关的东西。 因此,尽管该应用程序现在运行良好,但对这个问题的任何进一步见解都会受到欢迎。
答案 0 :(得分:0)
好吧,我在做了正确的搜索之后终于找到了什么问题,看看这里提供的答案。请注意,这是android中的一个错误,只发生在字符串集中。谢谢 Set<String> in android sharedpreferences does not save on force close