How to receive data ordered by date on firebase real-time database?

和自甴很熟 提交于 2020-12-27 06:09:42

问题


I have an Android app that let the user books appointments with date & time pickers, when User books an appointment, it records the appointment date & time on the firebase real-time database as a string like this (29/01/2020 - 10:30), I want to receive all the appointments that is saved on the firebase from another activity but sorted by date (newest at top)
For example it should be sorted like this:
3/1/2021 9:00
9/12/2020 10:30
8/11/2020 17:00
8/11/2020 15:30
6/10/2020 10:30
6/10/2020 10:00
and so on..


回答1:


After a lot of search I've found an easy way to do this, Which is to add an string value attribute to firebase real-time database with date & time picked from User but in this format:
yyyy/MM/dd- HH:mm
Notes to get this approach works:
1- Save the Hour with 24-hr format (e.g: 16:00 not 4:00 PM), You can change it later while receiving it to 12-hr format.
2- You should save (Month, Day, Hour & Minutes) with two digits not one, e.g (2020/03/09 - 01:05) instead of (2020/3/9 - 1:5), in other coding words, any number less than 10 prefix it with 0, I've coded a method for this:

private String formatDateAndTimeByZero(int appointmentMonth, int appointmentDay,
                                       int appointmentHour, int appointmentMinute) {
    String appSortMonth = "", appSortDay = "", appSortHour = "", appSortMin = "",
            fullDateFormatted;

    if(appointmentMonth < 10){
        appSortMonth = "0" + appointmentMonth;
    }else{
        appSortMonth = String.valueOf(appointmentMonth);
    }

    if(appointmentDay < 10){
        appSortDay = "0" + appointmentDay;
    }else{
        appSortDay = String.valueOf(appointmentDay);
    }

    if(appointmentHour < 10){
        appSortHour = "0" + appointmentHour;
    }else{
        appSortHour = String.valueOf(appointmentHour);
    }

    if(appointmentMinute < 10){
        appSortMin = "0" + appointmentMinute;
    }else{
        appSortMin = String.valueOf(appointmentMinute);
    }

    fullDateFormatted = appointmentYear + "/" + appSortMonth + "/" + appSortDay +
            " - " + appSortHour + ":" + appSortMin;

    return fullDateFormatted;
}

Then you can push the returned string in this method to firebase to be like this:

After that you can receive your data ordered by date like this:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Appointments");
Query query = reference.orderByChild("appointmentDateForSort");

I think this works because it's like checking for the highest number from left to right first, if it's the same (e.g same year), it goes more into the right and checks, until it finds not equaled values, then it takes the higher value of the comparison of the two different value and retrieve it, You can reverse the stack list order later in order to put the newest date at the top by following this answer.



来源:https://stackoverflow.com/questions/64677530/how-to-receive-data-ordered-by-date-on-firebase-real-time-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!