首页 > 解决方案 > 如何将 listView 的值存储在 ArrayList 中

问题描述

这是我的代码,但结果是最后一行出现多次

var listViewOrd: ListView? = null
listViewOrd = findViewById(R.id.List_Ordonance_add)
    var medicamentOrdonance = MedicamentOrdonance()
    var listMedicamentOrdonance =ArrayList<MedicamentOrdonance>()


 for (pos in listViewOrd!!) {
                    var nameMedicamentList = findViewById<TextView>(R.id.name_medicament_list)
                    val test = nameMedicamentList.text.toString()
                    medicamentOrdonance.nameMedicament = test
                    listMedicamentOrdonance.add(medicamentOrdonance)
                }```

标签: androidkotlin

解决方案


HashSet如果这是您的用例,可用于确保您只有一个唯一的项目列表。

但是,如果您正在使用HashSet自定义对象,例如您的情况,请MedicamentOrdonance确保覆盖equals()hashCode()以便 HashSet 可以确定两个 MedicamentOrdonance 是否真的相同或不同。

然后,如果您需要 ArrayList 中的数据,您可以执行以下操作new ArrayList<MedicamentOrdonance>(hashSet);


推荐阅读