android - 如何将 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)
}```
解决方案
HashSet
如果这是您的用例,可用于确保您只有一个唯一的项目列表。
但是,如果您正在使用HashSet
自定义对象,例如您的情况,请MedicamentOrdonance
确保覆盖equals()
,hashCode()
以便 HashSet 可以确定两个 MedicamentOrdonance 是否真的相同或不同。
然后,如果您需要 ArrayList 中的数据,您可以执行以下操作new ArrayList<MedicamentOrdonance>(hashSet);
推荐阅读
- robotframework - Robot Framework 是否支持 Gherkin 数据表?
- string - 用 SQLite 中的某些字符替换部分字符串
- qt - 如何创建具有 2 种离散颜色的圆形边框?
- git - 在合并和推送期间忽略文件
- python - 从外部访问托管在 Azure VM(Windows 操作系统)上的 python 应用程序
- javascript - 如何在站点和json文件中添加应删除的删除按钮
- java - java 8中的.net列表分组?
- c# - 为什么在使用 Xamarin 表单打开画廊时调用 OnDisappearing() 方法
- grep - 如何从 grep 中排除一些匹配项?
- php - DDD / CQRS / ES - 如何以及在何处实施警卫