0

I have an array of strings (in strings.xml), in which I collect all my games

<string-array name="games">
    <item name="select" translatable="false">...</item>
    <item name="game_fortnite" translatable="false">Fortnite</item>
    <item name="game_csgo" translatable="false">CounterStrike: Global Offensive</item>
    <item name="game_minecraft_minigames" translatable="false">Minecraft Minigames</item>
</string-array>

I am now trying to get a specific item (In this case the first one, but I will need others later) from this array. Since i can give the items names, without Android Studio underlining it for me, I thought maybe i can refer to the strings by names, which does not work in any way I tried it.

In this case I am trying to find out wheter my string variable "game" has the same value as the array item I have named "select". I have tried all of the following:

if(game.equals(R.array.games.select)){
}

if(game.equals(R.string.games.select)){
}

if(game.equals(R.array.games[0])){
}

if(game.equals(R.array.select)){
}

I heve tried, as you can see, using an index (didn't work), but I'd like a possibility to refer to them by their name property.

2 Answers 2

2

This will reach your xml string and then you can use java string,

String gamesArr[] = getResources().getStringArray(R.array.games);

After doing this, create a list view and fill the list view items with that array.

And below code will give you selected item on the list view.

myList.getSelectedItem();

Sign up to request clarification or add additional context in comments.

Comments

0

As per android documentation, the item tag for string-array does not have any attribute available.

screenshot attached

https://developer.android.com/guide/topics/resources/string-resource#StringArray

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.