1

I am accessing Firebase on Android. I want to retrieve a child from the tree. My code is (updated)

         f.child("Germany").child("Username").child("Alan Turing").addChildEventListener(new ChildEventListener() {
        // Retrieve new posts as they are added to Firebase
        @Override
        public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
            Map<String, Object> newPost = (Map<String, Object>) snapshot.getValue();
            System.out.println("Author: " + newPost.get("Message"));
            disp_msg = (TextView)findViewById(R.id.display_msg);
            disp_msg.setText(newPost.get("Message").toString());
           // System.out.println("Title: " + newPost.get("title"));
        }

In my firebase I have

Language
  Germany
    Username
      Alan Turing
        Message
          -Jf6ShYy7niHrqg_x4Tc: "tom"
          -Jf9v0xHAxINUUANrORU: "perfect"

When I ran the code above I get:

Author: {
  Username={
    Alan Turing={
      Message={
        -Jf6ShYy7niHrqg_x4Tc=tom, 
        -Jf9v0xHAxINUUANrORU=perfect!
      }
    }
  }
}

But I want only the messages, not the entire Firebase database.

So how do I get just the specified child?

only this is working so far but it returns whole firebase:

         f.addChildEventListener(new ChildEventListener() {
        // Retrieve new posts as they are added to Firebase
        @Override
        public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
            Map<String, Object> newPost = (Map<String, Object>) snapshot.getValue();
            System.out.println("Author: " + newPost.get("Germany"));
            disp_msg = (TextView)findViewById(R.id.display_msg);
            disp_msg.setText(newPost.get("Germany").toString());
           // System.out.println("Title: " + newPost.get("title"));
        }

I stored it as follows:

Firebase usersRef = f.child("Language");
usersRef .child("Germany").child("Username").child("Alan Turing").child("Message").push().setValue(writeMsgField.getText().toString());
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
nothingness
  • 694
  • 3
  • 9
  • 25

1 Answers1

7

You can address child nodes directly , by constructing a Firebase reference off the root:

f.child("Germany")
 .child("Username")
 .child("Alan Turing")
 .child("Message")
 .addChildEventListener... 

or a bit more conciser:

f.child("Germany/Username/Alan Turing/Message").addChildEventListener.... 

You really should follow the Firebase Android guide, because the data structure is covered quite well in the topic on how data is structured in Firebase. Spending some time on that guide now will save you much time (and many questions) later.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • doesnt give my anything its just blank, has the child alan turing with space have any thing to do with this? – nothingness Jan 08 '15 at 21:10
  • I can access `ref.child('Alan Turing/Message').once('value', function(s) { console.log(JSON.stringify(s.val())); })` without any problems with Firebase's JavaScript API, so expect the Java API to work the same. More likely your data structure is different. See if you can get it from your Firebase dashboard or with the JavaScript snippet I just gave and **add** it to your question. – Frank van Puffelen Jan 08 '15 at 21:44
  • The way I store them is like this: f.child("Germany").child("Username").child("Alan Turing").child("Message").push().setValue(writeMsgField.getText().toString()); – nothingness Jan 09 '15 at 01:08
  • Cool. Please add this information to your question. Also ensure that the information was stored correctly, by checking your Firebase dashboard. – Frank van Puffelen Jan 09 '15 at 01:12
  • 1
    I checked the dashboard everything saved looks like exact as the structure in my question. It could be that I must save the data as a map ( I used child) in order to retrieve it as a map? – nothingness Jan 09 '15 at 01:18
  • That could well be. I only looked at the "how can I access children?" part of your question. But you can easily try if you can now get the children by using the opposite structure of how you set them. – Frank van Puffelen Jan 09 '15 at 01:19
  • It didnt work even with hashmap I will look for more info around stackoverflow – nothingness Jan 09 '15 at 02:50
  • I think my problem is I cant enter f.child("Germany/Username/Alan Turing/Message").addChildEventListener It wouldn't read anything in this method when I put in the path – nothingness Jan 09 '15 at 03:31
  • Even when I got inside the path i get an error about the child event. But why does it work without any path when I put Germany as the get child.. – nothingness Jan 09 '15 at 03:56
  • I change the map to the first tutorial and it worked but when i print it still contains the brackets { and the keys -JfBZu8MUqpDegzf0TG8 how do I go about getting rid of these? – nothingness Jan 09 '15 at 15:04