There is an array:

final String[] texts = {"Математика","Музыка (Пение)","Изобразительное искусство (Рисование)","Английский язык","Казахский язык", "Немецкий язык","Китайский язык","История","География","Биология","Химия"} 

I use it very often in my applications, how to save it once and call it from any place

    1 answer 1

    Move it to constants.

     import ...; import ...; public class MainActivity extends Activity { public static final String[] texts = {"Математика","Музыка (Пение)","Изобразительное искусство (Рисование)","Английский язык","Казахский язык", "Немецкий язык","Китайский язык","История","География","Биология","Химия"} 

    Call by the name of the variable texts . For example, to show the second element of the array in the Toast message

     Toast.makeText(this, texts[1], 0).show(); 

    Ie, the name of the array of texts and the index in square brackets (serial number minus one). For example, if you need the first element "Mathematics", then the index will be 0 , and so on.

    And it’s better to make an array in res/values/array.xml - be ready for localization (not all your users know Russian)

     <string-array name="texts"> <item>Биография</item> <item>Русский Язык</item> <item>......</item> ... </string-array> 

    And in the Activity code

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

    Good luck!

    • Thank you very much for the answer! - OPTIMIST .KZ
    • four
      Not "such a chip ..", but the indices of lists (including arrays) start from zero. So in almost all programming languages, and is associated with the practicality of application for data sampling. The index of an item is an offset from the beginning of the list, not a sequence number. - pavlofff
    • Thanks, corrected :) - Flippy