简体   繁体   English

StringSet不会保存到SharedPreferences或从中加载

[英]StringSets won't save/load to/from SharedPreferences

My app stores an ArrayList of custom Food objects which are posted to a ListView in the main Activity. 我的应用程序存储了一个自定义Food对象的ArrayList,这些对象已发布到主Activity中的ListView中。 The ListView portion of the app works perfectly, but I'm having an issue saving and loading these Food objects to and from SharedPreferences. 该应用程序的ListView部分运行正常,但是在将这些Food对象与SharedPreferences进行加载和加载时出现了问题。

I'm saving these Food objects to SharedPreferences by saving each of its attributes as a String, putting them in a StringSet, and saving each one via the putStringSet function. 我通过将每个Food对象的每个属性另存为String,将它们放在StringSet中,然后通过putStringSet函数保存每个Food对象,将它们保存到SharedPreferences中。 When the app resumes, it reads through each StringSet from SharedPreferences and creates a new Food item with the read attributes, then adds each one to the ArrayList. 当应用恢复时,它将从SharedPreferences中读取每个StringSet,并创建一个具有read属性的新Food项,然后将每个项添加到ArrayList中。 The code seems like it should work, but just doesn't load the data between sessions. 该代码似乎应该可以工作,但是不会在会话之间加载数据。 Can anyone help me figure out why? 谁能帮我找出原因? Thank you! 谢谢!

Code removed

You seem to never instantiate foodNamesSet variable. 您似乎从未实例化foodNamesSet变量。 Hence the check if(foodNamesSet != null) always return false. 因此,检查if(foodNamesSet!= null)始终返回false。 Same thing applies to other sets as well. 同样的情况也适用于其他集合。 Besides, I think you should do the logic in onPause() rather in onStop(). 此外,我认为您应该在onPause()中执行逻辑,而不是在onStop()中执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM