简体   繁体   English

Android Array 列表过滤器和分组具有相同键值的项目

[英]Android Array list filter and group items which have the same key value

I got this data from firebase and I want to group the items based on the deadline key value and get an array of grouped data.我从 firebase 获得了这些数据,我想根据截止日期键值对项目进行分组并获得一组分组数据。

{
  "-MJTP2ZF4-Qrn3xdK3x4" : {
    "comment" : "Clean ",
    "deadline" : "9/11/2020",
    "firstDeadline" : "12/10/2020",
    "interval" : 7,
    "owner" : "Mika",
    "parentTaskID" : "-MJTP2ZCNDXlgMsw6bHy",
    "subtask" : true,
    "title" : "Some "
  },
  "-MJTP2ZF4-Qrn3xdK3x5" : {
    "comment" : "Clean ",
    "deadline" : "16/11/2020",
    "firstDeadline" : "12/10/2020",
    "interval" : 7,
    "owner" : "Mika",
    "parentTaskID" : "-MJTP2ZCNDXlgMsw6bHy",
    "subtask" : true,
    "title" : "Some "
  },
  "-MJTPy8Q7MyGM5mO0b_N" : {
    "comment" : "7.3 C",
    "deadline" : "9/11/2020",
    "firstDeadline" : "12/10/2020",
    "interval" : 7,
    "owner" : "Mika",
    "parentTaskID" : "-MJTPy8PdX4DmWWC_DfE",
    "subtask" : true,
    "title" : "Temp"
  },
  "-MJTPy8Q7MyGM5mO0b_O" : {
    "comment" : "7.3 C",
    "deadline" : "16/11/2020",
    "firstDeadline" : "12/10/2020",
    "interval" : 7,
    "owner" : "Mika",
    "parentTaskID" : "-MJTPy8PdX4DmWWC_DfE",
    "subtask" : true,
    "title" : "Temp"
  },
  "-MLHTHmzx8J7Ifh0Mecx" : {
    "comment" : "Finally ",
    "deadline" : "4/11/2020",
    "owner" : "Mika",
    "repetition" : false,
    "title" : "Clean "
  },
  "-MLHTx_ZK4n5dhkBb8TE" : {
    "comment" : "Dirty ",
    "deadline" : "04/12/2020",
    "owner" : "Mika",
    "repetition" : false,
    "title" : "Clean "
  }
}

and I want to group items based on the deadline key and got something like this我想根据截止日期键对项目进行分组并得到这样的东西

{

    "9/11/2020":{
        {
  "-MJTP2ZF4-Qrn3xdK3x4" : {
    "comment" : "Clean ",
    "deadline" : "9/11/2020",
    "firstDeadline" : "12/10/2020",
    "interval" : 7,
    "owner" : "Mika",
    "parentTaskID" : "-MJTP2ZCNDXlgMsw6bHy",
    "subtask" : true,
    "title" : "Some "
  },
    "-MJTPy8Q7MyGM5mO0b_N" : {
    "comment" : "7.3 C",
    "deadline" : "9/11/2020",
    "firstDeadline" : "12/10/2020",
    "interval" : 7,
    "owner" : "Mika",
    "parentTaskID" : "-MJTPy8PdX4DmWWC_DfE",
    "subtask" : true,
    "title" : "Temp"
  },
    },

    "16/11/2020":{
         "-MJTPy8Q7MyGM5mO0b_O" : {
    "comment" : "7.3 C",
    "deadline" : "16/11/2020",
    "firstDeadline" : "12/10/2020",
    "interval" : 7,
    "owner" : "Mika",
    "parentTaskID" : "-MJTPy8PdX4DmWWC_DfE",
    "subtask" : true,
    "title" : "Temp"
  },
"-MJTPy8Q7MyGM5mO0b_O" : {
    "comment" : "7.3 C",
    "deadline" : "16/11/2020",
    "firstDeadline" : "12/10/2020",
    "interval" : 7,
    "owner" : "Mika",
    "parentTaskID" : "-MJTPy8PdX4DmWWC_DfE",
    "subtask" : true,
    "title" : "Temp"
  }
    },
    "04/12/2020":{
        "-MLHTx_ZK4n5dhkBb8TE" : {
    "comment" : "Dirty ",
    "deadline" : "04/12/2020",
    "owner" : "Mika",
    "repetition" : false,
    "title" : "Clean "
  },
  "-MLHTHmzx8J7Ifh0Mecx" : {
    "comment" : "Finally ",
    "deadline" : "4/11/2020",
    "owner" : "Mika",
    "repetition" : false,
    "title" : "Clean "
  }
    }
}

and this is how I got the data from the firebase I just iterate them and store array list then I want that item list not to be just a list of items but grouped and have a chunk of the items array list这就是我从 firebase 获取数据的方式我只是迭代它们并存储数组列表然后我希望该项目列表不仅仅是一个项目列表而是分组并有一个项目数组列表

 ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot singleSnapshot : snapshot.getChildren()) {
                    Task task = singleSnapshot.getValue(Task.class);
                    mTaskArrayList.add(task);
                }
                mLogBookRecyclerView.setAdapter(mAdapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        };
        mDatabaseReference.addValueEventListener(valueEventListener);

The data structure you want to use is a Map, specifically a HashMap.您要使用的数据结构是 Map,特别是 HashMap。

I assumed your Task class has a getter getDeadline for the deadline date and it is a String.我假设你的Task类有一个用于截止日期的 getter getDeadline ,它是一个字符串。 If you are using one of the many Java inbuilt Date features, modify the class accordingly.如果您正在使用许多 Java 内置日期功能之一,请相应地修改该类。

Map<String,List<Task>> groupedTasks=new HashMap<>();
.
.
.
public void onDataChange(@NonNull DataSnapshot snapshot) {
        
        for (DataSnapshot singleSnapshot : snapshot.getChildren()) {
            Task task = singleSnapshot.getValue(Task.class);
            if(!groupedTasks.containsKey(task.getDeadline())
                groupedTasks.put(task.getDeadline(),new ArrayList<Task>());
            groupedTasks.get(task.getDeadline()).add(task);
        }
        mLogBookRecyclerView.setAdapter(mAdapter);
}

You can later access them using the get method of the Map.您可以稍后使用 Map 的get方法访问它们。 For example groupedData.get("16/11/2020") will return a List<Task> containing the Tasks of that deadline.例如groupedData.get("16/11/2020")将返回一个List<Task>包含该截止日期的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 8 Lambda表达式:将满足给定谓词的所有项目按顺序分组。 分组应以谓词为键,项目为值 - Java 8 lambda expression: Group all items in sequence which satisfy the given predicate. Grouping shall have predicate as key, items as value 按键的特定值过滤对象的数组列表 - Filter array list of objects by specific value of key Android 过滤器项目列表 - Android filter list of items 分组 JSON 数组数据,如果有两个相同的值,则计数 - Group JSON array data and count it if have two same value 如何更改与具有相同键的HashMap列表中的键关联的值? - How to change the value associated with a key in a list of HashMaps that have same Keys? 如果列表项在一个变量中具有相同的值,则使用不同的变量比较列表 - Compare list using a different variable if list items have the same value in one variable Android通过键(日期)订购Firebase项目 - Android order Firebase items by key which is a date 从对象列表中删除另一个对象中不存在键值的项目 - Remove items from a list of objects which key value doesn't exist in another one 如何在Android中更改/编辑具有键和值对属性的xml的节点值? - how to change /edit node value of xml which have an attribute with key and value pair in android? 在LinkedList中对具有相同值的项目进行排序??? -Java - Sorting items that have the same value in LinkedList??? - java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM