在Firebase android中搜索两个日期内的子值

时间:2018-05-01 13:18:53

标签: java android firebase firebase-realtime-database

这就是json的样子

"Node" : {
      "1-5-2018" : {
        "10001" : {
          "centre" : "centre_1",
          "name" : "name_1",
          "paidAmt" : "25000"
        },
        "10002" : {
          "centre" : "centre_2",
          "name" : "name_2",
          "paidAmt" : "25000"
        },
        "10003" : {
          "centre" : "centre_3",
          "name" : "name_3",
          "paidAmt" : "10000"
        }
        },
      "2-5-2018 : {
        "10004" : {
            "centre" : "centre_4",
            "name" : "name_4",
            "paidAmt" : "20000"
            }
        }

我想在给定日期内检索值(center,name和paidAmt)

我已尝试使用此特定查询,但始终显示数据不存在

Query query = databaseReference1.orderByKey().startAt(fromDate.getText().toString()).endAt(toDate.getText().toString());

                query.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if(dataSnapshot.exists()) {
                            Log.d(TAG, "Data exists within dates");
                            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                                Log.d(TAG , "Data are " + ds.getValue().toString());
                            }
                        }
                        else {
                            Log.d(TAG, "Data does not exist within dates");
                        }
                    }

如果可以检索日期内的所有数据,请告知我们。将来,我计划在不同日期内添加多个数据,并希望根据需求检索它们。也许每月或每周。

1 个答案:

答案 0 :(得分:1)

您正在使用时间戳而不是可能看起来像这样的日期。

"Node" : {
      "1525188857" : {
        "10001" : {
          "centre" : "centre_1",
          "name" : "name_1",
          "paidAmt" : "25000"
        },
        "10002" : {
          "centre" : "centre_2",
          "name" : "name_2",
          "paidAmt" : "25000"
        },
        "10003" : {
          "centre" : "centre_3",
          "name" : "name_3",
          "paidAmt" : "10000"
        }
        },
      "1525188873" : {
        "10004" : {
            "centre" : "centre_4",
            "name" : "name_4",
            "paidAmt" : "20000"
            }
        }