【问题标题】:Listview bringing one item only from the string array in strings.xmlListview 仅从 strings.xml 中的字符串数组中获取一项
【发布时间】:2016-10-23 04:53:17
【问题描述】:

我正在尝试在 android 中创建一个ListView,但它只在列表中生成一项。

这是我的代码 sn-p

String[] kampala1 = getResources().getStringArray(R.array.kla);
sitelistview.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
                        android.R.layout.simple_list_item_activated_1,kampala1));

我需要帮助来获取我创建的字符串数组中的所有字符串

字符串数组是

<string-array name="kla"> 
   <item>uganda Museum</item>
   <item>Namugongo Museum</item>
   <item>wandegeya Market</item>
   <item>kasubi tombs</item>
   <item>kabaka palace</item>
   <item>kabaka lake</item>
</string-array>

【问题讨论】:

  • 你的意思是kampala1 只包含一项?
  • 请告诉我们您在strings.xml中的字符串数组
  • 你的数组看起来是正确的,所以看起来 getResources 和 setAdapter。也许你应该给我们更多的信息。该代码放在哪里?在 onCreate 中?

标签: android arrays listview


【解决方案1】:

如果要使用硬编码字符串数组,则直接使用xml。

像这样在 strings.xml 中声明你的数组。

<string-array name="kla">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>

并在你的布局中使用它。

 <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:entries="@array/kla"/>

【讨论】:

    【解决方案2】:

    您可以使用数组适配器。它是一个很好的解决方案,具有足够的灵活性和定制空间。检查以下解决方案中解释的示例。

    http://stackoverflow.com/questions/2453989/help-in-getting-string-array-from-arrays-xml-file

    【讨论】:

      猜你喜欢
      • 2016-12-02
      • 2011-11-14
      • 2011-11-30
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多