问题
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