简体   繁体   中英

How to access <item> from <string-array> resource in an android XML?

I have a xml file (/res/xml/setting.xml) for PreferenceActivity:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory android:title="Main Settings">
            <ListPreference
                android:title="Background Image"
                android:summary="Set the background image"
                android:key="key_background"
                android:entries="@array/background"
                android:entryValues="@array/background_values" 
                android:defaultValue="winter.png" /> 
        </PreferenceCategory>
</PreferenceScreen>

Then I have another xml file "/res/values/string.xml":

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="background">
        <item>Winter</item>
        <item>Desert</item>
    </string-array>

    <string-array name="background_values">
        <item>winter.png</item>
        <item>desert.png</item>
    </string-array>

</resources>

See ListPreference in setting.xml, I want android:defaultValue to be set with winter.png . But I also don't want to be set with hardcoded/constant value in the xml, so I tried with various values such as " @array/background_values/0 ", " @array/background_values[0] ", etc...but all failed.

So, the questions are:

  1. What is the correct syntax for accesing an item of string-array resource in other xml?
  2. How to make sure if the android:defaultValue is working?
  3. Is there any documentation about the @array syntax? I can't found any.
  1. Android Developers gives no way for resource reference to string-array in XML, not to metion its items.
  2. You can clear your app's data, reinstall and open your app, go to your settings screen, then the defaultValue will go to your shared prefereces file. If defaultValue matches one of the items, it will be selected.

BTW, you can change your items in string-array to reference to string to avoid hardcoding.

Use the index of the value you want from your array android:defaultValue="0"

The above code worked only because i am using numbers inside my string array values, it is not because of index.

1- Define a separate file for arrays (res/values/arrays.xml)

arrays.xml

<?xml version="1.0" encoding="utf-8"?
<resources>
<string-array name="settings_order_by_labels">
    <item>label1</item>
    <item>label2</item>
</string-array>

<string-array name="settings_order_by_values">
    <item>value1</item>
    <item>value2</item>
</string-array>
</resources>

2- use this arrays in Preference xml file:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/settings_title">

<ListPreference
    android:defaultValue="value1"
    android:entries="@array/settings_order_by_labels"
    android:entryValues="@array/settings_order_by_values"
    android:key="order_by"
    android:title="Order By" />
</PreferenceScreen>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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