How to access xml array string list after structure

I create the following structure. Here the levels are up to 4, and the groups are up to 6 identical XML file structures. But I can not access the first element level1 โ†’ group1->, which also refers to the element. Here, the general level has 4, and each level has 6 separate groups, and each individual group has 10 separate objects.

<resources> <string-array name="level1"> <item> <string-array name="group1"> <item> <string-array> <item>the</item> <item>the little boy</item> </string-array> </item> <item> <string-array> <item>a</item> <item>a good boy</item> </string-array> </item> </string-array> </item> <item> <string-array name="group2"> <item> <string-array> <item>he</item> <item>he is it</item> </string-array> </item> <item> <string-array> <item>i</item> <item>i can go</item> </string-array> ... </item> ... </string-array> ... </item> ... </string-array> 

Here is the code I'm trying ....

  final String levels []=getResources().getStringArray(R.array.level); final TextView tw=(TextView)findViewById(R.id.txtWord); String group1=levels[0]; final String groups []=getResources().getStringArray(R.array.group); String item1=groups[0]; tw.setText(item1); 

So, could you give me any ideas on this issue. My ultimate goal: select Level1-> Group1-> Then. Press the next button and show 1 word at the same time and several times (never finish or in a circular way).

+4
source share
2 answers

So you want to iterate over a group of strings? Why not use simple rows with indexes?

 <string name="string_deftype">string</string> <string name="mylexicon_identifier">level_%1$d_group_%2$d_word_%3$d</string> <string name="level_1_group_1_word_1">the</string> <string name="level_1_group_1_word_2">the little boy</string> .... <string name="level_2_group_2_word_1">i</string> <string name="level_2_group_2_word_2">i can go</string> 

And then in your activity or context

 this.getResources().getIdentifier(getString(R.string.mylexicon_identifier, index_level, index_group, index_word), getString(R.string.string_deftype), getApplicationContext().getPackageName())); 

So what you can basically do with this:

 loop levels loop groups loop words 

If you need to check your cycles, if there is an appropriate resource (look in the documentation)

 getIdentifier() != 0 

Keep in mind that this approach is not very fast, as indicated in the documentation. Hope this helps.

PS: You can also use the database, of course. ^^

+1
source

I do not believe that the resources of a string array should work this way.

0
source

All Articles