gpt4 book ai didi

java - SharedPreferences Android 中奇怪的无法解释的行为

转载 作者:行者123 更新时间:2023-11-30 01:37:02 28 4
gpt4 key购买 nike

我对 sharedPreferences 并不陌生,我决定使用它们来存储用户创建的类(class)的标题,然后加载到 listView 中。类(class)标题存储在一个集合中,然后放入 sharedPreference。这里的问题是当我输入第一门类(class)(作为用户)时,它会被永久保存,但添加更多类(class)也会保存,并且在应用程序处于 Activity 状态时工作,但在应用程序重新启动时,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();
}
}

}

附言。我省略了一些不必要的代码,如 UI 实现和其他非 sharedPreference 相关的东西。因此,即使该应用程序现在运行良好,我们也非常欢迎对此问题有任何进一步的见解。

最佳答案

经过适当的搜索,我终于找到了问题所在,请查看此处提供的答案。请注意,这是 android 中的一个错误,并且只发生在字符串集上。谢谢 Set<String> in android sharedpreferences does not save on force close

关于java - SharedPreferences Android 中奇怪的无法解释的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34988775/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com