SophisticOtter
SophisticOtter

Reputation: 3

Get a specific string from a stringarray in Android Studio

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.

Upvotes: 0

Views: 2189

Answers (2)

Kanika
Kanika

Reputation: 61

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

Upvotes: 0

Emre Tufekci
Emre Tufekci

Reputation: 157

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();

Upvotes: 2

Related Questions