简体   繁体   English

Android:如何编写Firebase实时数据库查询?

[英]Android : How to write Firebase realtime database query?

{
  "City" : {
    "New York" : {
      "City Name" : "New York",
      "Place to visit" : {
        "Times Square" : {
          "Address" : "Somewhere",
          "Name" : "Times Square"
        },
        "Central Park" : {
          "Address" : "On America",
          "Name" : "Central Park"
        }
      }
    },
    "Los Angeles" : {
      "City Name" : "Los Angeles",
      "Place to visit" : {
        "Hollywood" : {
          "Address" : "Up There",
          "Name" : "Hollywood"
        },
        "Beach" : {
          "Address" : "By the sea",
          "Name" : "Beach"
        }
      }
    }
  }
}

Hi, I'm a little bit new to NoSQL database especially Firebase. 嗨,我是NoSQL数据库的新手,尤其是Firebase。 If I structured my data like this, how can I get the name of "place to visit" where "City name" is New York? 如果我这样构造数据,如何获得“城市名”为“纽约”的“游览地点”的名称?

And I want the result (Times Square and Central Park) to be shown as ListView. 我希望结果(时代广场和中央公园)显示为ListView。 How can I achieve that? 我该如何实现?

Or is there any better way to restructure my data so I can query my desired output easier? 还是有什么更好的方法来重组我的数据,以便我可以更轻松地查询所需的输出?

First of all city should hold a list of items (in your case city name "New York or Los Angeles"). 首先,城市应该保存一个项目清单(在您的情况下,城市名称为“ New York or Los Angeles”)。

By this it will be easier for you to traverse through the list and fetch desired city as required in a fastest and efficient manner, this is because you will get inbuilt methods to traverse. 这样,您将更容易遍历列表,并以最快,最有效的方式根据需要获取所需的城市,这是因为您将获得内置的遍历方法。

I have also restructured your json response in order to make it a list of objects and removed redundant variables 我还对您的json响应进行了重组,以使其成为对象列表并删除了冗余变量

Below is the response 以下是回应

{
"City": [{
            "New York": {
                "Place to visit": {
                    "Times Square": {
                        "Address": "Somewhere",
                        "Name": "Times Square"
                    },
                    "Central Park": {
                        "Address": "On America",
                        "Name": "Central Park"
                    }
                }
            }
        },
        {
            "Los Angeles": {
                "Place to visit": {
                    "Hollywood": {
                        "Address": "Up There",
                        "Name": "Hollywood"
                    },
                    "Beach": {
                        "Address": "By the sea",
                        "Name": "Beach"
                    }
                }
            }
        }
    ]
}

Because the name of the city which you want to use in your query is already a node in your Firebase database, to get those names, please use the following code: 因为您要在查询中使用的城市名称已经是Firebase数据库中的一个节点,所以要获取这些名称,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference placeToVisitRef = rootRef.child("City").child("New York").child("Place to visit");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String placeToVisit = ds.getKey();
            Log.d("TAG", placeToVisit);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
placeToVisitRef.addListenerForSingleValueEvent(eventListener);

Your output will be: 您的输出将是:

Times Square
Central Park

Try this one, 试试这个

 DatabaseReference ref=   FirebaseDatabase.getInstance().getReference();
 DatabaseReference refPlace= rootRef.child("City").child("New York").child("Place to visit");
 refPlace.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                  Iterator<DataSnapshot> dataSnapshotsChat = dataSnapshot.child("articleTags").getChildren().iterator();

            while (dataSnapshotsChat.hasNext()) {
             DataSnapshot dataSnapshotChild = dataSnapshotsChat.next();
           // check dataSnapshotChild, here you will get the details under Place to visit
      Object name = ds.child("Times Square").getValue(Object.class);
             }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM