用java实现动态排序_关于Java动态分组排序的问题(Android中需要将数据排序给RecyclerView使用)...
发布日期:2021-06-24 12:10:35 浏览次数:3 分类:技术文章

本文共 1330 字,大约阅读时间需要 4 分钟。

问题描述

现在有这样一个TestNotification里面定义了这些字段

private long time;

private String pkg;

private String content;

private String title;

然后我需要将这些字段里面的pkg也就是PackageName分组,可能有音乐包名的,新闻包名的各种应用发来的不同消息,我需要将这些消息根据相同的包名来分组,目前可以将整个集合里面的数据用map分组。

private ArrayList notificationArrayList = new ArrayList<>();

//添加消息,转换组

public void addGroupItem(TestNotification notification) {

//添加数据

notificationArrayList.add(notification);

//转换map

Map> resultMap = new LinkedHashMap<>();

for (TestNotification testN : notificationArrayList) {

String pkg = testN.getPkg();

if (resultMap.containsKey(pkg)) {

resultMap.get(pkg).add(testN);

} else {

List tmp = new LinkedList<>();

tmp.add(testN);

resultMap.put(pkg, tmp);

}

}

//遍历map

for (String key : resultMap.keySet()) {

//将key信息放入分组的头部

Level0Item level0Item = new Level0Item(key, "---聚合---");

List notificationList = resultMap.get(key);

//遍历key下面的各个value

for (TestNotification testNotification : notificationList) {

//将key下面的value加入可折叠列表的子类中

level0Item.addSubItem(new Level1Item(testNotification.getTitle(),

testNotification.getPkg(), testNotification.isExpandItem(), testNotification.getContent()));

}

entityList.add(level0Item);

}

//为recyclerView设置数据源,通知刷新界面

setNewData(entityList);

}

希望能够解决的问题:

目前这样做,只能每次将所有的消息实体类遍历,然后每次为recyclerView设置新的数据源,导致已经展开的item,每次刷新都会重新合上。有什么好的排序方式,能够让我单处刷新新来的一条TestNOtification,而不需要每次全部遍历?

ecee747344ceca05ccdbc46f1d21771d.png

转载地址:https://blog.csdn.net/weixin_32661831/article/details/114827158 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java中文jsp页面_java中文乱码解决之道(七)—–JSP页面编码过程
下一篇:mysql 索引表的应用_MySQL查询优化之索引的应用详解

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月11日 03时25分26秒