【问题标题】:How Access the String array list xml following structure如何访问字符串数组列表xml以下结构
【发布时间】:2011-08-05 09:35:10
【问题描述】:

我创建了以下结构。这里的级别最多为 4 个,组最多为 6 个相同的 xml 文件结构。但我不能作为 level1 -> group1-> first item 访问,该 item 也有。这里总共有 4 个级别,每个级别有 6 个单独的组,每个单独的组有 10 个单独的项目。

 <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>

这里有一些代码,我试试....

        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);

那么,请您给我这个问题背后的任何想法。我的最终目标是,选择 Level1->Group1-> 然后单击下一步按钮,一次显示 1 个单词并重复显示(永不完成或循环方式)。

【问题讨论】:

  • 有人不知道这个问题背后的原因吗????

标签: android xml arrays


【解决方案1】:

所以你想遍历一组字符串? 为什么不使用带索引的纯字符串?

<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>

然后在您的活动或上下文中

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

那么你基本上可以用这个做什么:

loop levels
  loop groups
    loop words

如果存在适当的资源,则必须检查循环(查看文档)

getIdentifier() != 0

请记住,这种方法并不是文档中提到的非常快的方法。 我希望这会有所帮助。

PS:当然你也可以使用数据库。^^

【讨论】:

    【解决方案2】:

    我不相信字符串数组资源打算以这种方式工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多