How to recover a string-array item within my Activity layout in the "xml" case?

Asked

Viewed 2,746 times

3

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

code in 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.

  • It would be easier, in order to give an answer, to ask the question, xml of the string array.

2 answers

2


The string-array shall be placed in the file /res/values/arrays.xml and not in /res/values/strings.xml

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

To explain I will use an example taken from this tutorial.

First define the 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, define the array in /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, in a Spinner you 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 array item you will not use the array but the string defined in /res/values/strings.xml.
This allows using the name assigned to the string and not an index.

For example 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);

1

You can load into an array of Strings:

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

And, to set, you can do through a simple interface, as:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.