It is necessary to make it so that the program can work with arrays from resources, but that the names of the arrays are not predetermined in advance in the code of the program itself.

I was advised to use HashMap , but I do not fully understand how to use it in my case.

  Map <String, String[]> hashmap = new HashMap<>(); int current = 1; 

In the resources we have an array of the format name (will be the same for all arrays) and number (will change).

  String wpqp = Integer.toString(current); hashmap.put (wpqp, "getResouces().getStringArray(R.array.somearray)" + Integer.toString(current)); 

I get the error that the String cannot be converted to String[] . Most likely, the solution is very simple, but I still can not find it on my own.

    1 answer 1

    First, create the name of your array, which is stored in the resources:

     String arrayName = "somearray" + current; 

    Then you get the resource identifier (in this case, the array of strings) by the generated name:

     int resId = getResources().getIdentifier(arrayName, "array", getPackageName()); 

    And then by the identifier you get the array itself:

     String[] array = getResources().getStringArray(resId); 

    All the above is true for the case when an array of the form is specified in strings.xml :

     <string-array name="somearray1"> <item>text_1</item> <item>text_2</item> <item>text_3</item> </string-array> 

    and current == 1 .

    • As I just did not try - the program still can not find the resource. Are you sure you wrote the code correctly? - FullyRetarded
    • @ user3807779, Updated the post. - post_zeew Nov.
    • And because of what it could be: android.content.res.Resources $ NotFoundException: String array resource ID # 0x0? It seems to have written everything correctly, checked everything, there are no typos - FullyRetarded
    • @ user3807779, Show all the code and content of the strings.xml file. - post_zeew Nov.
    • one
      @ user3807779, This line is String chastring = "R.array.chapter" + current; replace with this String chastring = "chapter" + current; . - post_zeew Nov. 2:57