开发者

How Access the String array list xml following structure

I have create following structure. Here levels are up to 4 and gr开发者_JS百科oups are up to 6 same xml file structure.But i cannot access as level1 -> group1-> first item, which has also to item. Here total level has 4 and each level has 6 individual group and each individual group has 10 individual items.

 <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 some code, that i try....

        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, would you please give me any ideas behind this problem. My ultimate goal is, Choose Level1->Group1-> Then Click the next button and show up 1 word at time and repeatedly(Never finish or circular way).


So you want to iterate over a group of strings? Why not using plain strings with indices?

<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 basically can do with this:

loop levels
  loop groups
    loop words

Whereas you have to check in your loops if the appropriate resource exists (look into the documentation)

getIdentifier() != 0

Keep in mind, that this approach is not a very fast one as mentioned in the documentation. I hope this helps.

PS: You could also use a database of course.^^


I do not believe string array resources were intended to work this way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜