Android - ListPreference 保存到文件或在条目单击监听器上?

发布于 2024-11-02 10:56:08 字数 865 浏览 6 评论 0原文

我的应用程序有一个首选项文件“settings”,其中包含 10 个键/值对。

键充当用户的标题,值是 URL。

键和值都可以由用户更改,例如,当用户更改该设置时,第一个设置看起来像“example”,值为“example.com” ,密钥也发生变化。因此,第一个设置将变为“ different_example”,其值为“ different_example.com”。全部存储在“设置”首选项文件下。

到目前为止,我一直在管理这个问题,方法是打开一个包含 ArrayList 中当前键/值对的对话框,该对话框具有一个 onItemClickListener,弹出第二个对话框,其中包含另一个可能的键/值对的 ArrayList。单击新项目时,我会删除当前设置,添加新设置,然后使用新设置重新填充初始 ArrayList。这很有效,并且允许同时更改和更新键和值,但是这两个对话框看起来很尴尬。

我想将其全部切换到 ListPreferences。例如,有十个不同的 ListPreference 项目,每个设置一个,单击时会打开所有可能的 EntryValues 的列表,选择时会更新条目名称中的键和条目值中的值,并将其保存在相同的“设置”文件。我不知道如何将 ListPreferences 保存到特定文件,以便我可以

SharedPreferences settings = getSharedPreferences("settings", 0);

在任何地方调用,尽管

我也一直在寻找某种单击处理程序来在选择条目时触发什么,以便我可以手动更新“设置”文件,但没有任何运气。这样的事存在吗?或者我还有其他方法可以做到这一点吗?

编辑:我可以使用 OnPreferenceChange 手动设置新值,但这不会返回值名称,例如人类可读列表中使用的值。关于如何获得它有什么想法吗?

My application has a preference file, "settings", which contains 10 key/value pairs.

The keys act as titles for the user, and the values are URL's

Both the key and the value are changeable by the user e.g. the first setting looks something like "example" with the value "example.com", when a user changes that setting, the key also changes. So the first setting would become "different_example" with the value "different_example.com". All stored under the "settings" preference file.

I have been managing this so far, by opening a dialog containing the current key/value pairs in an ArrayList that has an onItemClickListener that pops up a second dialog containing another ArrayList of the possible key/value pairs. When the new item is clicked, I remove the current setting, add the new one, then re-populate the initial ArrayList with the new settings. This works and allows both the key and value to be simultaneously changed and updated, however it looks awkward with the two dialogs.

I'd like to switch this all over to ListPreferences. As in, have ten different ListPreference items, one for each setting, that when clicked opens the listing of all possible entryValues, and when selected, updates the key from the entry name, and the value from the entry value, and saves this under the same "settings" file. I'm not seeing how to save ListPreferences to a specific file, so that I can call

SharedPreferences settings = getSharedPreferences("settings", 0);

anywhere, though

I've also been looking for some kind of click handler for what to fire when an entry is selected so I can manually update the "settings" file, but not having any luck. Does such a thing exist? Or is there another way for me to do this?

Edit: I can use OnPreferenceChange to manually set the new value, but this doesn't return the value name, e.g. the value used in the human-readable list. Any ideas on how to get that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风启觞 2024-11-09 10:56:09

看看这是否可以让您快速开始:我如何获得在 Android 中工作的首选项?

如果您自定义 ListPreference 并遇到类似这样的内容 如何使首选项不保存到 SharedPreferences?

See if this can give you a jump start: How do I get preferences to work in Android?

If you customize your ListPreference and come across something like this How to make a Preference not save to SharedPreferences?

回忆追雨的时光 2024-11-09 10:56:09

啊,这看起来令人难以置信的倒退,但我所做的是将每个 ListPreference 设置为有一个 onPreferenceChangeListener,并且 ListPreference 的每个 EntryValue 包含名称以及由任意字符串分隔的值。然后在 onPreferenceChange 中,我可以引用新值(现在也包含新键)将其转换为任意分隔符处的 String[] 分割,然后将 String[] 0 索引指定为新键,将 1 索引指定为新键删除原始设置后,使用 SharedPreferences.Editor 作为新值。

除非有某种方法可以从 ListPreference 的 entryValues 数组返回 Entry 名称,或者指定 ListPreference 保存到特定的设置文件(这两个文件都没有记录),那么这可能是对我来说最好的解决方案。

Ahh, well this seems incredibly backwards, but what I've done is set each ListPreference to have an onPreferenceChangeListener, and each entryValue for the ListPreference to contain the name as well as the value separated by an arbitrary string. Then in the onPreferenceChange, I can reference the new value (which now also contains the new key) turn it into a String[] split at the arbitrary separator, then assign the String[] 0 index as the new key, and the 1 index as the new value by using SharedPreferences.Editor after removing the original setting.

Unless there's some way to return the Entry name from the ListPreference's entryValues array, or to specify ListPreference to save to a specific settings file, neither of which is documented, then this will probably be the best solution for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文