简体   繁体   中英

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. 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. 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").

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

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:

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) {

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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