android - 在 listView 中合并相同项目的最佳方法是什么?
问题描述
我有lisView
一些可能具有相似属性的物品,我想要:
首先,找到这些具有相似属性的项目。
第二,将这些合并到列表视图的一个行项中,
和
最后,当项目被选中时,在展开的布局中显示它们的项目(合并的)。
我的想法不是那么强烈,我想使用for
循环来找到相似的项目,然后将它们存储在 an 中array
,最后adapter
检查是否项目是合并的type
然后使用Expandable Layout
.
有更好的解决方案吗?
解决方案
考虑使用 HashMap。
HashMap<String, ArrayList<Item>> map = new HashMap<String, ArrayList<Item>>();
您的属性是字符串或可以比较的任何内容,并且作为值,它将存储您的项目列表。为此,请使用以下内容:
for (int i = 0; i < listOfItems.size(); i++) {
if(map.containsKey(listOfItems.get(i).property){
map.get(listOfItems.get(i).property).add(listOfItems.get(i));
}else{
ArrayList temp = new ArrayList();
temp.add(listOfItems.get(i);
map.put(listOfItems.get(i).property, listOfItems.get(i))
}
}
并获取具有相同属性的对象列表,请使用
map.get(property)
推荐阅读
- c# - Lucene.net 4.8 - IDE 不能识别 Lucene.Net.Standard 以及 StandardAnalyzer 的
- python - 无法在其他 PC 上运行转换后的 exe
- c# - 构造函数参数的自定义 Android.Gms.Maps.Model.TileProvider 问题
- highcharts - highchart:“分组类别” mutil 类别显示不漂亮
- indexeddb - 无法在“IDBObjectStore”上执行“放置”:函数(根)....更新...
... } 无法克隆 - php - 我正在玩测验,如果用户不在数据库中,我想创建新用户
- linux - 使用 --enable-kernel=VERSION 为非常旧的内核(早于 3.2.0)编译 glibc(2.28)是否有任何解决方法?
- c# - DataTables MVC on Postback 数据为空
- passport.js - 如何修复“运行`getDataFromTree`时出错网络错误:未定义localStorage”?
- django - psycopg2 2.8.3 版本中的多处理失败