Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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 嵌套循环多次添加同一元素_Android_Kotlin_Arraylist_Android Recyclerview - Fatal编程技术网

Android 嵌套循环多次添加同一元素

Android 嵌套循环多次添加同一元素,android,kotlin,arraylist,android-recyclerview,Android,Kotlin,Arraylist,Android Recyclerview,我有两个数组,分别是字符串数组和整数数组,如下所示: val country= resources.getStringArray(R.array.country_array) val flags = resources.getIntArray(R.array.country_flag_array) <string-array name="country_array"> <item>USA</item> <

我有两个
数组
,分别是
字符串数组
整数数组
,如下所示:

   val country= resources.getStringArray(R.array.country_array)
   val flags = resources.getIntArray(R.array.country_flag_array)
<string-array name="country_array">
    <item>USA</item>
    <item>France</item>
    <item>Canada</item>

</string-array>

<integer-array name="country_flag_array">
    <item>@drawable/flag_united_states_of_america</item>
    <item>@drawable/flag_france</item>
    <item>@drawable/flag_canada</item>

</integer-array>
它们位于my
strings.xml
中,如下所示:

   val country= resources.getStringArray(R.array.country_array)
   val flags = resources.getIntArray(R.array.country_flag_array)
<string-array name="country_array">
    <item>USA</item>
    <item>France</item>
    <item>Canada</item>

</string-array>

<integer-array name="country_flag_array">
    <item>@drawable/flag_united_states_of_america</item>
    <item>@drawable/flag_france</item>
    <item>@drawable/flag_canada</item>

</integer-array>
我有此功能来填充带有国旗和国家的
列表

private fun addCountriesToList(country: Array<String>, flgs : IntArray) : List<CountryObject>{

        val listCountry= ArrayList<CountryObject>()

        for(c in country){
            for (f in flgs){
                listCountry.add(CountryObject(f,  c))
            }
        }

        return listCountry
    }
我有另一个填充回收器视图的函数(我知道它是有效的)

我可以用国家名称填充
recyclerView
,但每个国家名称都填充了数组的大小,并且图像不会显示。因为有3个国家,每个国家有3次人口


我们将非常感谢您提供的任何帮助或建议,以了解为什么语言显示为那样,而不是图像。

您正在为每个国家为每个国旗添加同一个国家。您需要做的只是为每个国家添加匹配的索引标志

val countryObjects = country.indexedMap { index, country ->
    CountryObject(flgs[index], country)
}
这里的映射是一个迭代器,它接受一个参数(一个函数),其结果将构成列表的元素。这里的函数与lambda进行了语法化,参数是当前元素和元素本身的索引。lambda正在返回您的
CountryObject
,因为lambda上的最后一个声明就是返回

在本例中,您可以这样做,因为您在字符串和整数上有一个匹配的国家索引。更好的解决方案是枚举

enum class Country(val flagImage : Int, val country: String) {
    USA(/*the values here*/)
    //the other countries
}

然后您可以像这样直接使用enum
Country.values()

嵌套这些循环没有意义,因为您只需要每个循环中的每个元素中的一个。如果嵌套它们,则将得到的结果数相乘

解决此问题的方法是按索引迭代一次:

for(i in 0 until min(country.size, flgs.size)) {
    listCountry.add(CountryObject(flgs[i], country[i]))
}
您还可以使用快捷方式:
zip
功能:

val listCountry = flgs.zip(country) { f, c -> CountryObject(f, c) }
或者更简洁地说:

val listCountry = flgs.zip(country, ::CountryObject)

有趣的是,文档也显示了一个速记版本,你可以添加它@cutiko抱歉,我不确定你在那里指出了什么,我没有提到。文档中有一个速记
listA zip listB
,我认为这会增加答案的价值,这会给你一个配对列表,这不是OP想要的。非常感谢你的解释。非常有用。我用它来显示各个国家。你知道为什么国旗的图像不会被设置吗?这可能与数据类型有关吗?我认为您正在做一些无效的
@
注释通常会按照名称空间的指示进行解析。添加一个日志并查看数组中的值。通常,我会推送
Enum
,并使用
R.
中的值。然后,您可以使用
ContextCompat
将其传递给
数据类
作为可绘制文件进行求解
val listCountry = flgs.zip(country, ::CountryObject)