How to retrieve a string-array item within the layout of my activity, in the "xml" case?

3

I created an array of strings in xml in my folder strings.xml, now I want to know how to retrieve a specific item according to its position, both in the Java source itself and in the XML code ...

code in the strings folder:

<string-array name="string-array">
    <item>item01</item>
    <item>item02</item>
    <item>item03</item>
    <item>item04</item>
    <item>item05</item>
    <item>item06</item>
    <item>item07</item>
    <item>item08</item>
    <item>item09</item>
    <item>item09</item>
</string-array>

I would like to insert one of these items into my TextView .

    
asked by anonymous 11.04.2015 / 14:09

2 answers

2

String-array should be placed in the file /res/values/arrays.xml and not /res/values/strings.xml

Each element of the array should not be hard-coded in the array, but instead use a reference to a string defined in /res/values/strings.xml .

To explain, I'll use an example from this tutorial .

First define strings in /res/values/strings.xml .

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string
        name="app_name">Choose Your Own</string>
    <string
        name="race_orc">Orc</string>
    <string
        name="race_elf">Elf</string>
    <string
        name="race_troll">Troll</string>
    <string
        name="race_human">Human</string>
    <string
        name="race_halfling">Halfling</string>
    <string
        name="race_goblin">Goblin</string>
</resources>

Then, using these strings , set the array to /res/values/arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array
        name="races_array">
        <item>@string/race_goblin</item>
        <item>@string/race_orc</item>
        <item>@string/race_elf</item>
        <item>@string/race_human</item>
        <item>@string/race_troll</item>
        <item>@string/race_halfling</item>
    </string-array>
</resources>

In Java to get a reference to string-array use:

String[] cRaces = getResources().getStringArray(R.array.races_array);

In xml, for example, a Spinner can use it this way:

<Spinner
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:id="@+id/spinnerOfCharacterRaces"
     android:entries="@array/races_array">
</Spinner>

To access a particular item in the array will not use the array but the string defined in /res/values/strings.xml . This allows you to use the name assigned to the string and not an index.

For example on a button:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/race_orc" />

Or in java:

String race = getResources().getText(R.string.race_orc);
    
11.04.2015 / 15:51
1

You can load an array of Strings :

String[] mArray = getResources().getStringArray(R.array.string-array);

And, to be sure, you can do it through a simple interface like:

for (String mString : mArray){
    if (mString.equals("texto")){
        mTxtView.setText(mString);
    }
}
    
11.04.2015 / 15:37