【问题标题】:How to put ArrayList elements (items) into a TextView?如何将 ArrayList 元素(项目)放入 TextView?
【发布时间】:2015-04-11 02:13:50
【问题描述】:

我的 strings.xml 中有一个 ArrayList。我想从中选择元素并将其放入 .setText() 中。我该怎么做?

【问题讨论】:

  • 你需要让我们知道你到目前为止尝试了什么,粘贴一些你尝试过的代码......
  • 我有在 TextView 中设置字符串的代码,我有一个算法可以从数组中随机选择一个元素。我知道如何在 Java 中做到这一点,但我不知道如何从 XML 中的 ArrayList 中获取它。

标签: android arrays xml string textview


【解决方案1】:

示例:保存在 res/values/strings.xml 的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>
</resources>

此应用程序代码检索字符串数组:

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

将值分配给 textView:

    TextView textView = (TextView) findViewById(R.id.textView1); 
    textView.setText(planets[1]);  //Venus

阅读更多:

http://developer.android.com/guide/topics/resources/string-resource.html

【讨论】:

    【解决方案2】:

    ArrayList是否应该在arrays.xml中声明?如果是,使用Context.getResource().getStringArray()选择元素

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      相关资源
      最近更新 更多