0

我目前正在为我的偏好和数据使用 SharedPreferences。意识到通过 prefs.getAll() 获取数据的所有值实际上得到了我的两个 SharedPreferences,这非常烦人。

我的数据的最佳途径是什么。它是键值对,键是日期,值是浮点数。(实际上,理想情况下,每个键都有两个浮点数,但我可以遍历两个。)

我可以在我的活动中制作一个哈希映射并像往常一样膨胀/放气,并在我需要数据时将其发送到我的片段吗?

4

1 回答 1

1

Your question is asking a few different things, so I'll try answer them all.

For storing data that is too complex for shared preferences, you should look into using an SQLite database. There are some good libraries that make it very simple - check out ActiveAndroid or OrmLite.

If you want to stick with shared preferences, but want to solve the issue of getAll returning the preferences and the data, you can actually create 2 separate sharedpreferences. There is a method getSharedPreferences (String name, int mode) which takes a name. Use the default shared preferences for your preferences, and create a shared preference with a different name for your data.

As for sending data to your fragments, you can use a Bundle. Bundles take all sorts of data, and serializables as well, so that should be no problem. Put your data into a bundle and pass it to your fragment when its instantiated.

于 2013-10-23T00:02:20.787 回答