[英]How to count same value on recyclerview?
Is there any method for counting the occurrence of each item on recyclerview?有什么方法可以计算每个项目在 recyclerview 上的出现次数吗?
Lets say I have recyclerview items:假设我有 recyclerview 项目:
String[] array = {"item1","item1","item2","item2", "item3", "item4"};
expected output:-预期输出:-
item1 2
item2 2
item3 1
item4 1
I already create recycleview adapter, but i don't know how to count the same item.我已经创建了 recycleview 适配器,但我不知道如何计算相同的项目。 please help
请帮忙
Here is the function you are looking for这是您正在寻找的功能
public void countEntries(String[] items) {
HashMap itemToCountMap = new HashMap<String, Integer>();
for (String item : items) {
if (itemToCountMap.containsKey(item)) {
Integer current = (Integer) itemToCountMap.get(item);
itemToCountMap.put(item, current + 1);
} else {
itemToCountMap.put(item, 1);
}
}
// Key - is the string
// Value - is the number of repetitions
itemToCountMap.forEach((key, value) -> System.out.println("key: "+key+" value:"+value));
}
Now you only need to get items from the recyclerView.adapter
(if your items are public)现在您只需要从
recyclerView.adapter
获取项目(如果您的项目是公开的)
String[] items = recyclerView.getAdapter().getItems()
countEntries(items)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.