How to take an array from a string resource and place it in an array String? I tried it, did not work:

In class:

String[] planets = getResources().getStringArray(R.array.planet); 

String resource:

 <string-array name="planet"> <item>Марс</item> <item>Земля</item> <item>Юпитер</item> <item>Меркурий</item> </string-array> 

Closed due to the fact that the essence of the question is incomprehensible to the participants by Vladyslav Matviienko , aleksandr barakin , zRrr , user194374, cheops 5 Jul '16 at 17:21 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What does "not work" mean? - JuriySPb
  • 2
    Should work, I do likewise. Unless if it is a class, but not activity, then it is necessary to get String from resources through context. I pass the context to the class and then I pull out resources from it: String[] notifireArray = context.getResources().getStringArray(R.array.notification_array); - Camel
  • lay out more code and error errors - Vladyslav Matviienko

2 answers 2

 // загрузка массива строк из res/values/arrays.xml в текстовое поле textStrings String[] names = getResources().getStringArray(R.array.names); for(int i = 0; i < names.length; i++) { textStrings.append("Name[" + i + "]: "+ names[i] + "\n"); } // загрузка массива целых чисел из res/values/arrays.xml в текстовое поле textDigits int[] digits = getResources().getIntArray(R.array.digits); for(int i = 0; i < digits.length; i++) { textDigits.append("Digit[" + i + "]: "+ digits[i] + "\n"); } 

    In string.xml

     <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="planet"> <item>Марс</item> <item>Земля</item> <item>Юпитер</item> <item>Меркурий</item> </string-array> </resources> 

    And in class:

     String[] planets = getResources().getStringArray(R.array.planet); 

    Just checked, everything works.

    • Error: at com.simulator.role.PlayGame. <Init> (PlayGame.java:20) - FLWRZ4U
    • You call this: planets [0], planets [1], etc.? - Vladimir VSeos
    • one
      @ user207012, you specified the file and line where you have an error. But, since we do not see your file and the type of error this information is completely useless. You need to specify the type of error, stackTrace and the string itself where it occurs. Without this, it is impossible to help you. - Yuriy SPb