【问题标题】:How to add String Array and Integer Array from XML into one Collection如何将 XML 中的字符串数组和整数数组添加到一个集合中
【发布时间】:2020-11-07 17:24:28
【问题描述】:

大家好,我想在回收站视图中创建类似的东西

Example Step

我已经有一个名为HomeStep的类

data class HomeStep(
    val icon: Int,
    val circle: Int,
    val textName: String)

我已经在string.xml 中将图标(绿色一)和圆圈存储为整数数组

<integer-array name="icon_drawble">
    <item>@drawable/icon_1</item>
    <item>@drawable/icon_2</item>
    <item>@drawable/icon_3</item>
    <item>@drawable/icon_4</item>
</integer-array>

<integer-array name="circle_drawble">
    <item>@drawable/circle_1</item>
    <item>@drawable/circle_2</item>
    <item>@drawable/circle_3</item>
    <item>@drawable/circle_4</item>
</integer-array>

我已经将图标和圆圈作为字符串数组存储在string.xml

<string-array name="list_step">
    <item>@string/step_1_text</item>
    <item>@string/step_2_text</item>
    <item>@string/step_3_text</item>
    <item>@string/step_4_text</item>
</string-array>

是否可以在 Collection 中添加我的整数数组和字符串数组?

val listHomeStep = ArrayList<HomeStep>()
    val layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
    val adapter = HomeStepAdapter()

    var icon: Int
    var circle: Int
    var textName: String

    var i = 0
    while (i < listHomeStep.size) {
        icon = resources.getIntArray(R.array.icon_drawble)[i]
        circle = resources.getIntArray(R.array.circle_drawable)[i]
        textName = resources.getStringArray(R.array.list_step)[i]
        i++
    }

    listHomeStep.addAll(icon, circle, textName)
    adapter.setDataList(listHomeStep)

【问题讨论】:

    标签: android arrays android-studio kotlin collections


    【解决方案1】:

    答案是肯定的,你可以在 Collection 中添加 Integer-array 或 String-array

    在你的情况下,你应该像这样一一插入HomeStep

        val listHomeStep = ArrayList<HomeStep>()
        val layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        val adapter = HomeStepAdapter()
    
        var icon: Int
        var circle: Int
        var textName: String
    
        var i = 0
        while (i < listHomeStep.size) {
            icon = resources.getIntArray(R.array.icon_drawble)[i]
            circle = resources.getIntArray(R.array.circle_drawable)[i]
            textName = resources.getStringArray(R.array.list_step)[i]
            val homeStep = HomeStep(icon, circle, textName)
            listHomeStep.add(homeStep)
            i++
        }
    
        adapter.setDataList(listHomeStep)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多