Nested keying in SharedPreferences?

感情迁移 提交于 2019-12-13 07:29:29

问题


I need to be able to access multiple settings using one key. For example:

<EditTextPreference
    android:key="@string/channel_url1"
    android:defaultValue="http://youtu.be/" />
<EditTextPreference
    android:key="@string/num_views1"
    android:defaultValue="0" />
<EditTextPreference
    android:key="@string/num_subs1"
    android:defaultValue="0" />

<EditTextPreference
    android:key="@string/channel_url2"
    android:defaultValue="http://you.tu/" />
<EditTextPreference
    android:key="@string/num_views2"
    android:defaultValue="0" />
<EditTextPreference
    android:key="@string/num_subs2"
    android:defaultValue="0" />

The above works as long as I keep each and every key unique. But what I would like instead is have the key name for num_views and num_subs identical for each channel, so that it would be easy to iterate through them in Java code (without resorting to naming conventions).

Is it possible to do that in Android's preferences XML? i.e. select a collection of a preferences (not necessarily of the same type!) using a single top-level key?


回答1:


As far as I know, this is not supported. Your best chance is probably to encode all the things you need into a XML or JSON object and save them to a string preference. But thats a neat amount of overhead in my opinion.

I'd say rather use SQLite as an alternative.



来源:https://stackoverflow.com/questions/7251020/nested-keying-in-sharedpreferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!