简体   繁体   English

Android-ListPreference保存到文件还是在条目上单击侦听器?

[英]Android - ListPreference save to file or on entry click listener?

My application has a preference file, "settings", which contains 10 key/value pairs. 我的应用程序有一个首选项文件“设置”,其中包含10个键/值对。

The keys act as titles for the user, and the values are URL's 键用作用户的标题,值是URL的

Both the key and the value are changeable by the user eg the first setting looks something like "example" with the value "example.com", when a user changes that setting, the key also changes. 用户可以更改键和值,例如,第一个设置的值类似于“ example”,其值为“ example.com”,当用户更改该设置时,键也将更改。 So the first setting would become "different_example" with the value "different_example.com". 因此,第一个设置将变为“ different_example”,其值为“ 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. 到目前为止,我一直在管理此问题,方法是在ArrayList中打开一个包含当前键/值对的对话框,该列表具有onItemClickListener,该对话框会弹出第二个对话框,其中包含另一个可能的键/值对的ArrayList。 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. 单击新项目后,我将删除当前设置,添加新的设置,然后使用新设置重新填充初始ArrayList。 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. 我想将其全部切换到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. 与之类似,有十个不同的ListPreference项,每个设置一个,单击该选项将打开所有可能的entryValue的列表,并在选中后从条目名称更新键,从条目值更新值,并将其保存在相同的“设置”文件。 I'm not seeing how to save ListPreferences to a specific file, so that I can call 我没有看到如何将ListPreferences保存到特定文件,以便可以调用

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, eg the value used in the human-readable list. 编辑:我可以使用OnPreferenceChange手动设置新值,但这不会返回值名称,例如,人类可读列表中使用的值。 Any ideas on how to get that? 关于如何获得任何想法?

See if this can give you a jump start: How do I get preferences to work in Android? 看看这是否可以让您快速入门: 如何获得在Android中工作的偏好设置?

If you customize your ListPreference and come across something like this How to make a Preference not save to SharedPreferences? 如果您自定义ListPreference并遇到类似这样的问题如何使首选项不保存到SharedPreferences?

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. 嗯,这似乎是令人难以置信的倒退,但是我所做的是将每个ListPreference设置为具有onPreferenceChangeListener,并将ListPreference的每个entryValue包含名称以及用任意字符串分隔的值。 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. 然后在onPreferenceChange中,我可以引用新值(现在也包含新键),将其转换为在任意分隔符处的String []拆分,然后将String [] 0索引分配为新键,并将1索引删除原始设置后,使用SharedPreferences.Editor作为新值。

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. 除非有某种方法可以从ListPreference的entryValues数组中返回Entry名称,或者指定ListPreference保存到特定的设置文件(两者均未记录),否则这可能是我的最佳解决方案。

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

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