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.
{
"-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
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.
I assumed your Task
class has a getter getDeadline
for the deadline date and it is a String. If you are using one of the many Java inbuilt Date features, modify the class accordingly.
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. For example groupedData.get("16/11/2020")
will return a List<Task>
containing the Tasks of that deadline.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.