【问题标题】:How to get firebase record between two particular dates?如何在两个特定日期之间获取 Firebase 记录?
【发布时间】:2021-04-24 06:18:10
【问题描述】:

我想从 Firebase Realtime Firebase DB 中获取患者在“CurrentDate”到 2 个月“PreviousDate”之间的病历。我面临的问题是日期正在进入我将通过调试代码检查的查询。这是我将获得“CurrentDate”和“PreviousDate”的代码:

    date = Calendar.getInstance().time;
    val dateFormat = SimpleDateFormat("d-M-yyyy")
    currentDate = dateFormat.format(date)
    cDate.text = currentDate //this gives me **24-4-2021**


    val calendar = Calendar.getInstance()
    calendar.add(Calendar.MONTH, -2)
    val preDate = calendar.time
    previousDate = dateFormat.format(preDate)
    pDate.text = previousDate /// this gives me **24-2-2021**

这是我为获取记录而编写的以下函数的代码:

private fun browseAppointmentByDate() {
    mFirebaseInstance = FirebaseDatabase.getInstance()
    appointmentDBReference = mFirebaseInstance!!.reference
    completeAppointArrayList = ArrayList()

    val ref = FirebaseDatabase.getInstance()
    ref.getReference("PatientChecked").child(currentUserID).orderByChild("date").startAt(previousDate)
            .endAt(currentDate)
            .addListenerForSingleValueEvent(object : ValueEventListener {

                override fun onDataChange(doctorList: DataSnapshot) {
                    try {
                        completeAppointArrayList?.clear()
                        for (eachDoctor in doctorList.children) {
                            Log.e("TAG", "onDataChange: " + eachDoctor.value.toString())
                            var patientsListModel: CheckPatientModel = eachDoctor.getValue(CheckPatientModel::class.java)!!
                            completeAppointArrayList!!.add(patientsListModel)
                        }

                        if (completeAppointArrayList == null || completeAppointArrayList!!.size == 0) {
                            txtNoCompAppointFound!!.visibility = View.VISIBLE
                            txtNoCompAppointFound.text = "You have no recent appointment!!"
                            pbCompAppointment!!.visibility = View.GONE
                        } else {
                            txtNoCompAppointFound.visibility = View.GONE
                            pbCompAppointment!!.visibility = View.GONE
                        }

                        mCompleteAppointmentAdapter = CompleteAppointmentAdapter(activity, completeAppointArrayList)
                        rvCompletedAppoint!!.adapter = mCompleteAppointmentAdapter


                    } catch (e: Exception) {
                        Log.e("TAG", "onDataChange: $e")

                    } catch (e: Exception) {
                        Log.e("TAG", "onDataLoad: $e")
                        pbCompAppointment!!.visibility = View.GONE
                    }
                }

                override fun onCancelled(error: DatabaseError) {
                    Log.w(ContentValues.TAG, "loadPost:onCancelled", error.toException())
                }
            })
}

这是我的 Firebase 数据库的结构,在当前日期和上一个日期之间,至少我会得到 23-4-2021 的记录,但它没有显示。

【问题讨论】:

    标签: android firebase kotlin firebase-realtime-database


    【解决方案1】:

    您的“日期”属性作为字符串存储在数据库中。这意味着当您调用“orderByChild("date")”时,结果将按lexicographically 排序。这意味着字符串在排序时不考虑任何类型的数值,尤其是在涉及日期时,即使日期包含数字“23-4-2021”。所以查询数据库时不能使用字符串值:

    .startAt(previousDate).endAt(currentDate)
    

    而不是 Timestamp 对象,并期望像日期一样表现。为了解决这个问题,您应该将数据库中“日期”属性的类型更改为时间戳类型,并按照我在以下帖子中的回答中的说明将其相应地添加到数据库中:

    一旦您进行了更改,您的代码可能会保持不变,您将获得所需的结果。

    【讨论】:

    • 好的,先生,我会照你说的做的。但是在查询之间查询之前将查询写为 ref.getReference("PatientChecked").child(currentUserID).orderByChild("date").equalTo(currentDate) 它会给我数据库记录
    • 是的,因为您指向的是单个准确的数据库记录。而且,是的,它会工作得很好。但是,当涉及到查询时,情况就不再了。由于答案中解释的原因,您应该查询时间戳范围,not 字符串值。试一试,告诉我它是否有效。
    猜你喜欢
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 2018-12-21
    • 1970-01-01
    • 2012-10-17
    • 2013-04-16
    • 2016-08-04
    相关资源
    最近更新 更多