Hello! The essence of what

there is the following code in string.xml

<string-array name="stroka_1"> <item>Текст1</item> <item>Текст2</item> <item>Текст3</item> </string-array> <string-array name="stroka_2"> <item>Текст11</item> <item>Текст22</item> <item>Текст33</item> </string-array> 

In Aktiviti I receive string resources

 String[] Stroki= getResources().getStringArray(R.array.stroka_2); 

Is there such a possibility in java, in the end instead of 2 put the variable

Example:

 String str = "1"; String[] Stroki= getResources().getStringArray(R.array.stroka_+str); 

The code naturally shows an error, but I think the idea is clear.

That would not manually register all the thongs

 R.array.stroka_1 R.array.stroka_2 R.array.stroka_3 R.array.stroka_4 

And just take the variable instead of these numbers

    2 answers 2

    Try this:

     int i = 1; String name = "stroka_"+ i; int holderint = getResources().getIdentifier(name, "array", this.getPackageName()); String[] items = getResources().getStringArray(holderint); 

    You just need to loop it to run through all i.

    A source

    • Thanks buddy !!! Everything Works - Artsait
    • @Artsait Not at all :) If it's not difficult for you, please mark my answer as correct by clicking the checkbox to the left of it. - iramm 1:01 pm

    Use string formatting (as in C). For example:

      <string name="my_str">Первая переменная:%1$d вторая переменная: %2$d</string> 

    Further in the code

      String str = String.format(getString(R.string.my_str), 1, 123); textView.setText(str); 

    It turns out: First variable: 1 second variable: 123

    • We are not talking about the substitution of values ​​in the output string, but about the dynamic formation of the string identifier (R.string.text1, R.string.text2, etc., for example). - pavlofff