Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Firebase查询以获取给定范围内的数据_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java Firebase查询以获取给定范围内的数据

Java Firebase查询以获取给定范围内的数据,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我在Firebase中有一个名为quotes的节点。在Android中获取特定范围的数据时,我遇到了一些问题。我想从2开始获取3个id。这是我的查询数据库: "quotes" : { "-L75elQJaD3EYPsd4oWS" : { "authorName" : "Hellen v", "famousQuote" : "When one door of happiness closes, another opens; but often we look so long at the

我在Firebase中有一个名为quotes的节点。在Android中获取特定范围的数据时,我遇到了一些问题。我想从2开始获取3个id。这是我的查询数据库:

"quotes" : {
"-L75elQJaD3EYPsd4oWS" : {
  "authorName" : "Hellen v",
  "famousQuote" : "When one door of happiness closes, another opens; but often we look so long at the closed door that we do not see the one which has been opened for us.",
  "id" : "1",
  "uploadedBy" : "Admin"
},
"-L7GOvDNI-o_H8RvNwoN" : {
  "authorName" : "Rocky Balboa",
  "famousQuote" : "It's not about how hard you can hit; it's about how hard you can get hit and keep moving forward.",
  "id" : "2",
  "uploadedBy" : "Admin"
},
"-L7GP9oBv5NR1T6HlDd4" : {
  "authorName" : "African proverb",
  "famousQuote" : "If you want to go fast, go alone. If you want to go far, go together.",
  "id" : "3",
  "uploadedBy" : "Admin"
},
"-L7GPjM1F3_7Orcz0Q1q" : {
  "authorName" : "A.P.J Abdul Kalam",
  "famousQuote" : "Don’t take rest after your first victory because if you fail in second, more lips are waiting to say that your first victory was just luck.",
  "id" : "4",
  "uploadedBy" : "Admin"
},
下面是我引用的规则

"quotes": {
 ".indexOn": ".value"
}

如何获取id为2、3和4的引号?

如果您的数据库中有4条以上的记录,要解决这个问题,您可以使用一个查询,其中您应该组合使用
startAt()
endAt()
方法来限制查询的两端,如下所示:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("quotes").orderByChild("id").startAt("2").endAt("4");
query.addListenerForSingleValueEvent(/* ... */);
Query queryTwo = rootRef.child("quotes").orderByChild("id").equalsTo("2");
queryTwo.addListenerForSingleValueEvent(
    List<Item> list = new ArrayList();
    list.add(itemTwo);
    Query queryThree = rootRef.child("quotes").orderByChild("id").equalsTo("3");
    queryThree.addListenerForSingleValueEvent(
        list.add(itemThree);
        Query queryFour = rootRef.child("quotes").orderByChild("id").equalsTo("4");
        queryFour.addListenerForSingleValueEvent(
            list.add(itemFour);

            //Do what you need to do with the list that contains three items
        );
    );
);
请参阅此处有关Firebase查询方法的更多信息:

使用给定的orderBy指令或优先级作为默认值,创建一个仅返回值大于或等于给定值的子节点的查询

以下是有关Firebase查询方法的更多信息:

使用给定的orderBy指令或优先级作为默认值,创建一个仅返回值小于或等于给定值的子节点的查询

编辑:根据您的评论,如果您只希望将
id
属性设置为2、3和4的项,则应使用如下嵌套查询:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("quotes").orderByChild("id").startAt("2").endAt("4");
query.addListenerForSingleValueEvent(/* ... */);
Query queryTwo = rootRef.child("quotes").orderByChild("id").equalsTo("2");
queryTwo.addListenerForSingleValueEvent(
    List<Item> list = new ArrayList();
    list.add(itemTwo);
    Query queryThree = rootRef.child("quotes").orderByChild("id").equalsTo("3");
    queryThree.addListenerForSingleValueEvent(
        list.add(itemThree);
        Query queryFour = rootRef.child("quotes").orderByChild("id").equalsTo("4");
        queryFour.addListenerForSingleValueEvent(
            list.add(itemFour);

            //Do what you need to do with the list that contains three items
        );
    );
);
查询queryTwo=rootRef.child(“引号”).orderByChild(“id”).equalsTo(“2”); queryTwo.addListenerForSingleValueEvent( 列表=新的ArrayList(); 增加(第二项); 查询querytree=rootRef.child(“引号”).orderByChild(“id”).equalsTo(“3”); QueryTree.addListenerForSingleValueEvent( 增加(第三项); 查询queryFour=rootRef.child(“引号”).orderByChild(“id”).equalsTo(“4”); queryFour.addListenerForSingleValueEvent( 增加(第四项); //对包含三项的列表执行所需操作 ); ); );
你能解释一下你试过什么,出了什么问题吗?你面临什么问题?我试过下面的问题1
queryquotes=mFirebaseDatabase.child(“quotes”).orderByValue.startAt(2)-这里我得到了引号中的所有节点。2
queryquotes=mFirebaseDatabase.child(“quotes”).orderByValue.startAt(2.limitToFirst(3)-这里我得到了前三名的结果。出了什么问题?你是4点才拿到的吗?或者否?
id
值存储为字符串,因此
.startAt(“2”).endAt(“4”)
@Alex,我的数据库中有500多条记录。当我使用
queryquery=rootRef.child(“引号”).orderByChild(“id”).startAt(“2”).endAt(“4”)我正在获取id为2,3和4的记录,同时还获取id为21,22,40,42的记录。。等等。我只需要2,3和4。@FrankvanPuffelen哦,糟糕,你说得对,Puf,刚刚更新了我的答案。如果您在将来看到我的代码中有任何其他不一致之处,请随时更正。谢谢如果你考虑搬到Cloud Firestore,你可以用更简单的方式来实现这一点,正如我在回答中所解释的。一切都好吧,我能帮你提供其他的信息吗?