Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Listview仅从strings.xml中的字符串数组中获取一项_Android_Arrays_Listview - Fatal编程技术网

Android Listview仅从strings.xml中的字符串数组中获取一项

Android Listview仅从strings.xml中的字符串数组中获取一项,android,arrays,listview,Android,Arrays,Listview,我试图在android中创建一个列表视图,但它只在列表中生成一项 这是我的代码片段 String[] kampala1 = getResources().getStringArray(R.array.kla); sitelistview.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_activa

我试图在android中创建一个
列表视图
,但它只在列表中生成一项

这是我的代码片段

String[] kampala1 = getResources().getStringArray(R.array.kla);
sitelistview.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
                        android.R.layout.simple_list_item_activated_1,kampala1));
String[]kampala1=getResources().getStringArray(R.array.kla);
sitelistview.setAdapter(新的ArrayAdapter(getApplicationContext()),
android.R.layout.simple_list_item_activated_1,坎帕拉1);
我需要帮助获取我创建的字符串数组中的所有字符串

字符串数组是

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

乌干达博物馆
纳穆贡戈博物馆
万德杰市场
卡苏比陵墓
卡巴卡宫
卡巴卡湖

如果要使用硬代码字符串数组,请直接使用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>

1.
2.
3.
4.
5.
6.
7.
8.
9
10
并在布局中使用它

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

如果要使用硬代码字符串数组,请直接使用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>

1.
2.
3.
4.
5.
6.
7.
8.
9
10
并在布局中使用它

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

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


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


您的意思是说
坎帕拉1
只包含一项?请向我们展示您在strings.xml中的字符串数组。您的数组看起来正确,getResources和setAdapter看起来也正确。也许你应该给我们更多的信息。代码放在哪里?在onCreate中?您的意思是说
kampala1
只包含一项?向我们展示您在strings.xml中的字符串数组。请您的数组看起来正确,getResources和setAdapter看起来也正确。也许你应该给我们更多的信息。代码放在哪里?在一次创建中?