FireBase Query. DataSnapshot











up vote
1
down vote

favorite












I need help on getting the "message" Object from this DataSnapshot result



DataSnapshot { key = user-4, value = {-JvFuwKX7r7o0ThXc0x8={sender=unit_owner, message=fkfkgkgkgkggkgkgkgkgkgkgkgkgkgkgkgkglgkgkgkgkgkgkgkg, role=unit_owner, profile_image=http://54.169.41.20/assets/boy-862a2e9036b094973c04afa1c0365a45.png, name=Paul Bartbartbart}} }


I am able to get the key value by getKey() and I have JvFuwKX7r7o0ThXc0x8 as an Object as well, this is an automatic FireBase generated name of the object. Instead of listening to the object itself. How can I navigate trough, "sender" or "message" and getting its value by using DataSnapshot member methods?










share|improve this question






















  • Can you share the JSON that you store in Firebase and the code that you use to retrieve it? I'm having a hard time parsing the Android output of it. I think I see the structure, but just want to make sure before typing an answer.
    – Frank van Puffelen
    Jul 29 '15 at 17:54















up vote
1
down vote

favorite












I need help on getting the "message" Object from this DataSnapshot result



DataSnapshot { key = user-4, value = {-JvFuwKX7r7o0ThXc0x8={sender=unit_owner, message=fkfkgkgkgkggkgkgkgkgkgkgkgkgkgkgkgkglgkgkgkgkgkgkgkg, role=unit_owner, profile_image=http://54.169.41.20/assets/boy-862a2e9036b094973c04afa1c0365a45.png, name=Paul Bartbartbart}} }


I am able to get the key value by getKey() and I have JvFuwKX7r7o0ThXc0x8 as an Object as well, this is an automatic FireBase generated name of the object. Instead of listening to the object itself. How can I navigate trough, "sender" or "message" and getting its value by using DataSnapshot member methods?










share|improve this question






















  • Can you share the JSON that you store in Firebase and the code that you use to retrieve it? I'm having a hard time parsing the Android output of it. I think I see the structure, but just want to make sure before typing an answer.
    – Frank van Puffelen
    Jul 29 '15 at 17:54













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I need help on getting the "message" Object from this DataSnapshot result



DataSnapshot { key = user-4, value = {-JvFuwKX7r7o0ThXc0x8={sender=unit_owner, message=fkfkgkgkgkggkgkgkgkgkgkgkgkgkgkgkgkglgkgkgkgkgkgkgkg, role=unit_owner, profile_image=http://54.169.41.20/assets/boy-862a2e9036b094973c04afa1c0365a45.png, name=Paul Bartbartbart}} }


I am able to get the key value by getKey() and I have JvFuwKX7r7o0ThXc0x8 as an Object as well, this is an automatic FireBase generated name of the object. Instead of listening to the object itself. How can I navigate trough, "sender" or "message" and getting its value by using DataSnapshot member methods?










share|improve this question













I need help on getting the "message" Object from this DataSnapshot result



DataSnapshot { key = user-4, value = {-JvFuwKX7r7o0ThXc0x8={sender=unit_owner, message=fkfkgkgkgkggkgkgkgkgkgkgkgkgkgkgkgkglgkgkgkgkgkgkgkg, role=unit_owner, profile_image=http://54.169.41.20/assets/boy-862a2e9036b094973c04afa1c0365a45.png, name=Paul Bartbartbart}} }


I am able to get the key value by getKey() and I have JvFuwKX7r7o0ThXc0x8 as an Object as well, this is an automatic FireBase generated name of the object. Instead of listening to the object itself. How can I navigate trough, "sender" or "message" and getting its value by using DataSnapshot member methods?







java android firebase






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 29 '15 at 15:30









Aizen

1,0852520




1,0852520












  • Can you share the JSON that you store in Firebase and the code that you use to retrieve it? I'm having a hard time parsing the Android output of it. I think I see the structure, but just want to make sure before typing an answer.
    – Frank van Puffelen
    Jul 29 '15 at 17:54


















  • Can you share the JSON that you store in Firebase and the code that you use to retrieve it? I'm having a hard time parsing the Android output of it. I think I see the structure, but just want to make sure before typing an answer.
    – Frank van Puffelen
    Jul 29 '15 at 17:54
















Can you share the JSON that you store in Firebase and the code that you use to retrieve it? I'm having a hard time parsing the Android output of it. I think I see the structure, but just want to make sure before typing an answer.
– Frank van Puffelen
Jul 29 '15 at 17:54




Can you share the JSON that you store in Firebase and the code that you use to retrieve it? I'm having a hard time parsing the Android output of it. I think I see the structure, but just want to make sure before typing an answer.
– Frank van Puffelen
Jul 29 '15 at 17:54












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










I looks like you're adding a value listener to something that has multiple children. I'm guessing it's a limitToLast(1) query, but it would be really helpful if you include the code in your question. Until you do add it however, this is my guess of what your code looks like:



Firebase ref = new Firebase("https://yours.firebaseio.com");
Query messages = ref.orderByKey().limitToLast(1);
messages.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i("Messages", dataSnapshot.toString());
}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
})


While you are limiting the filter to single message, you are still a list of messages. It's just a list of only 1 message.



This means that on onDataChange you will still need to drill down into the child. Since you cannot do that by index, you will need to use a for loop:



public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot chatSnapshot: dataSnapshot.getChildren()) {
String message = (String) chatSnapshot.child("message").getValue();
String sender = (String) chatSnapshot.child("sender").getValue();
}
}


Note that this gets a lot more readable if you take a moment to create a Java class that represents the message:



public class Chat {
String message;
String sender;
public String getMessage() { return message; }
public String getSender() { return sender; }
}


Then you can get the message with:



public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot chatSnapshot: dataSnapshot.getChildren()) {
Chat chat = chatSnapshot.getValue(Chat.class);
String message = chat.getMessage();
String sender = chat.getSender();
}
}


The above snippet still needs the for loop, since you're still getting a list of 1 messages. If you want to get rid of that loop, you can register a child event listener:



query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Chat chat = dataSnapshot.getValue(Chat.class);
String message = chat.getMessage();
String sender = chat.getSender();
}

// other overridden messages have been removed for brevity

})


Firebase will call the onChildAdded() method for each child, so you don't have to look over them yourself.






share|improve this answer





















  • This is the Best answer Detailed and clear. From all the queries I have seen in FireBase Tag. The only thing missing here is the List Approach. Which I actually use from your example Class. BTW the Class getValue Example that I tried, is sending me an error Bounce Type. So what I did to fix it is, put Static on my Class. I HAVE NO IDEA WHY IT WORKED.
    – Aizen
    Jul 29 '15 at 19:36










  • Hmmm... that sounds interesting, I've never had to make the class static, but I never use inner classes for these anyway. Can you open a new question for that debouncing problem? Post the JSON, you Java class and the code how you read it.
    – Frank van Puffelen
    Jul 29 '15 at 20:00










  • Sure, I will add that question. For later. For Now I am having a problem Trying to stop getting all of the collection, FireBase on Fire will throw me all of the Collection. Is it something to do with putting the Chat Class inside a Service? (Not an Intent Service). What I did is, I added the Chat Class where my Service Class is, and the FireBase Listeners are also in there. I will have to open up a Question about this, because I am not sure what might happen in the future.
    – Aizen
    Jul 29 '15 at 20:13










  • I can't say from your description. Post a question with a minimal-but-complete repro of the problem and I'll have a look. See stackoverflow.com/help/mcve
    – Frank van Puffelen
    Jul 29 '15 at 21:24






  • 1




    Incredibly enough somebody else today reached out with the same problem with an inner class. The reason is clear btw. A non-static inner class has an implicit reference to its containing class. And that's a field that Firebase cannot initialize for you when it deserializes. In other words: "bla bla bla... it must be static". :-)
    – Frank van Puffelen
    Jul 30 '15 at 0:18


















up vote
1
down vote













The value will be returned as an Object, as you've stated, but you can actually cast it to Map<String, Object> since you know it should be a map.



So, do something like this:



Map<String, Object> val = (Map<String, Object>) dataSnapshot.getValue();
String message = (String) val.get("message");
String sender = (String) val.get("sender");





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31704859%2ffirebase-query-datasnapshot%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    I looks like you're adding a value listener to something that has multiple children. I'm guessing it's a limitToLast(1) query, but it would be really helpful if you include the code in your question. Until you do add it however, this is my guess of what your code looks like:



    Firebase ref = new Firebase("https://yours.firebaseio.com");
    Query messages = ref.orderByKey().limitToLast(1);
    messages.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    Log.i("Messages", dataSnapshot.toString());
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
    })


    While you are limiting the filter to single message, you are still a list of messages. It's just a list of only 1 message.



    This means that on onDataChange you will still need to drill down into the child. Since you cannot do that by index, you will need to use a for loop:



    public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot chatSnapshot: dataSnapshot.getChildren()) {
    String message = (String) chatSnapshot.child("message").getValue();
    String sender = (String) chatSnapshot.child("sender").getValue();
    }
    }


    Note that this gets a lot more readable if you take a moment to create a Java class that represents the message:



    public class Chat {
    String message;
    String sender;
    public String getMessage() { return message; }
    public String getSender() { return sender; }
    }


    Then you can get the message with:



    public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot chatSnapshot: dataSnapshot.getChildren()) {
    Chat chat = chatSnapshot.getValue(Chat.class);
    String message = chat.getMessage();
    String sender = chat.getSender();
    }
    }


    The above snippet still needs the for loop, since you're still getting a list of 1 messages. If you want to get rid of that loop, you can register a child event listener:



    query.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    Chat chat = dataSnapshot.getValue(Chat.class);
    String message = chat.getMessage();
    String sender = chat.getSender();
    }

    // other overridden messages have been removed for brevity

    })


    Firebase will call the onChildAdded() method for each child, so you don't have to look over them yourself.






    share|improve this answer





















    • This is the Best answer Detailed and clear. From all the queries I have seen in FireBase Tag. The only thing missing here is the List Approach. Which I actually use from your example Class. BTW the Class getValue Example that I tried, is sending me an error Bounce Type. So what I did to fix it is, put Static on my Class. I HAVE NO IDEA WHY IT WORKED.
      – Aizen
      Jul 29 '15 at 19:36










    • Hmmm... that sounds interesting, I've never had to make the class static, but I never use inner classes for these anyway. Can you open a new question for that debouncing problem? Post the JSON, you Java class and the code how you read it.
      – Frank van Puffelen
      Jul 29 '15 at 20:00










    • Sure, I will add that question. For later. For Now I am having a problem Trying to stop getting all of the collection, FireBase on Fire will throw me all of the Collection. Is it something to do with putting the Chat Class inside a Service? (Not an Intent Service). What I did is, I added the Chat Class where my Service Class is, and the FireBase Listeners are also in there. I will have to open up a Question about this, because I am not sure what might happen in the future.
      – Aizen
      Jul 29 '15 at 20:13










    • I can't say from your description. Post a question with a minimal-but-complete repro of the problem and I'll have a look. See stackoverflow.com/help/mcve
      – Frank van Puffelen
      Jul 29 '15 at 21:24






    • 1




      Incredibly enough somebody else today reached out with the same problem with an inner class. The reason is clear btw. A non-static inner class has an implicit reference to its containing class. And that's a field that Firebase cannot initialize for you when it deserializes. In other words: "bla bla bla... it must be static". :-)
      – Frank van Puffelen
      Jul 30 '15 at 0:18















    up vote
    3
    down vote



    accepted










    I looks like you're adding a value listener to something that has multiple children. I'm guessing it's a limitToLast(1) query, but it would be really helpful if you include the code in your question. Until you do add it however, this is my guess of what your code looks like:



    Firebase ref = new Firebase("https://yours.firebaseio.com");
    Query messages = ref.orderByKey().limitToLast(1);
    messages.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    Log.i("Messages", dataSnapshot.toString());
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
    })


    While you are limiting the filter to single message, you are still a list of messages. It's just a list of only 1 message.



    This means that on onDataChange you will still need to drill down into the child. Since you cannot do that by index, you will need to use a for loop:



    public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot chatSnapshot: dataSnapshot.getChildren()) {
    String message = (String) chatSnapshot.child("message").getValue();
    String sender = (String) chatSnapshot.child("sender").getValue();
    }
    }


    Note that this gets a lot more readable if you take a moment to create a Java class that represents the message:



    public class Chat {
    String message;
    String sender;
    public String getMessage() { return message; }
    public String getSender() { return sender; }
    }


    Then you can get the message with:



    public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot chatSnapshot: dataSnapshot.getChildren()) {
    Chat chat = chatSnapshot.getValue(Chat.class);
    String message = chat.getMessage();
    String sender = chat.getSender();
    }
    }


    The above snippet still needs the for loop, since you're still getting a list of 1 messages. If you want to get rid of that loop, you can register a child event listener:



    query.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    Chat chat = dataSnapshot.getValue(Chat.class);
    String message = chat.getMessage();
    String sender = chat.getSender();
    }

    // other overridden messages have been removed for brevity

    })


    Firebase will call the onChildAdded() method for each child, so you don't have to look over them yourself.






    share|improve this answer





















    • This is the Best answer Detailed and clear. From all the queries I have seen in FireBase Tag. The only thing missing here is the List Approach. Which I actually use from your example Class. BTW the Class getValue Example that I tried, is sending me an error Bounce Type. So what I did to fix it is, put Static on my Class. I HAVE NO IDEA WHY IT WORKED.
      – Aizen
      Jul 29 '15 at 19:36










    • Hmmm... that sounds interesting, I've never had to make the class static, but I never use inner classes for these anyway. Can you open a new question for that debouncing problem? Post the JSON, you Java class and the code how you read it.
      – Frank van Puffelen
      Jul 29 '15 at 20:00










    • Sure, I will add that question. For later. For Now I am having a problem Trying to stop getting all of the collection, FireBase on Fire will throw me all of the Collection. Is it something to do with putting the Chat Class inside a Service? (Not an Intent Service). What I did is, I added the Chat Class where my Service Class is, and the FireBase Listeners are also in there. I will have to open up a Question about this, because I am not sure what might happen in the future.
      – Aizen
      Jul 29 '15 at 20:13










    • I can't say from your description. Post a question with a minimal-but-complete repro of the problem and I'll have a look. See stackoverflow.com/help/mcve
      – Frank van Puffelen
      Jul 29 '15 at 21:24






    • 1




      Incredibly enough somebody else today reached out with the same problem with an inner class. The reason is clear btw. A non-static inner class has an implicit reference to its containing class. And that's a field that Firebase cannot initialize for you when it deserializes. In other words: "bla bla bla... it must be static". :-)
      – Frank van Puffelen
      Jul 30 '15 at 0:18













    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    I looks like you're adding a value listener to something that has multiple children. I'm guessing it's a limitToLast(1) query, but it would be really helpful if you include the code in your question. Until you do add it however, this is my guess of what your code looks like:



    Firebase ref = new Firebase("https://yours.firebaseio.com");
    Query messages = ref.orderByKey().limitToLast(1);
    messages.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    Log.i("Messages", dataSnapshot.toString());
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
    })


    While you are limiting the filter to single message, you are still a list of messages. It's just a list of only 1 message.



    This means that on onDataChange you will still need to drill down into the child. Since you cannot do that by index, you will need to use a for loop:



    public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot chatSnapshot: dataSnapshot.getChildren()) {
    String message = (String) chatSnapshot.child("message").getValue();
    String sender = (String) chatSnapshot.child("sender").getValue();
    }
    }


    Note that this gets a lot more readable if you take a moment to create a Java class that represents the message:



    public class Chat {
    String message;
    String sender;
    public String getMessage() { return message; }
    public String getSender() { return sender; }
    }


    Then you can get the message with:



    public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot chatSnapshot: dataSnapshot.getChildren()) {
    Chat chat = chatSnapshot.getValue(Chat.class);
    String message = chat.getMessage();
    String sender = chat.getSender();
    }
    }


    The above snippet still needs the for loop, since you're still getting a list of 1 messages. If you want to get rid of that loop, you can register a child event listener:



    query.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    Chat chat = dataSnapshot.getValue(Chat.class);
    String message = chat.getMessage();
    String sender = chat.getSender();
    }

    // other overridden messages have been removed for brevity

    })


    Firebase will call the onChildAdded() method for each child, so you don't have to look over them yourself.






    share|improve this answer












    I looks like you're adding a value listener to something that has multiple children. I'm guessing it's a limitToLast(1) query, but it would be really helpful if you include the code in your question. Until you do add it however, this is my guess of what your code looks like:



    Firebase ref = new Firebase("https://yours.firebaseio.com");
    Query messages = ref.orderByKey().limitToLast(1);
    messages.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    Log.i("Messages", dataSnapshot.toString());
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
    })


    While you are limiting the filter to single message, you are still a list of messages. It's just a list of only 1 message.



    This means that on onDataChange you will still need to drill down into the child. Since you cannot do that by index, you will need to use a for loop:



    public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot chatSnapshot: dataSnapshot.getChildren()) {
    String message = (String) chatSnapshot.child("message").getValue();
    String sender = (String) chatSnapshot.child("sender").getValue();
    }
    }


    Note that this gets a lot more readable if you take a moment to create a Java class that represents the message:



    public class Chat {
    String message;
    String sender;
    public String getMessage() { return message; }
    public String getSender() { return sender; }
    }


    Then you can get the message with:



    public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot chatSnapshot: dataSnapshot.getChildren()) {
    Chat chat = chatSnapshot.getValue(Chat.class);
    String message = chat.getMessage();
    String sender = chat.getSender();
    }
    }


    The above snippet still needs the for loop, since you're still getting a list of 1 messages. If you want to get rid of that loop, you can register a child event listener:



    query.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    Chat chat = dataSnapshot.getValue(Chat.class);
    String message = chat.getMessage();
    String sender = chat.getSender();
    }

    // other overridden messages have been removed for brevity

    })


    Firebase will call the onChildAdded() method for each child, so you don't have to look over them yourself.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jul 29 '15 at 18:17









    Frank van Puffelen

    221k25362387




    221k25362387












    • This is the Best answer Detailed and clear. From all the queries I have seen in FireBase Tag. The only thing missing here is the List Approach. Which I actually use from your example Class. BTW the Class getValue Example that I tried, is sending me an error Bounce Type. So what I did to fix it is, put Static on my Class. I HAVE NO IDEA WHY IT WORKED.
      – Aizen
      Jul 29 '15 at 19:36










    • Hmmm... that sounds interesting, I've never had to make the class static, but I never use inner classes for these anyway. Can you open a new question for that debouncing problem? Post the JSON, you Java class and the code how you read it.
      – Frank van Puffelen
      Jul 29 '15 at 20:00










    • Sure, I will add that question. For later. For Now I am having a problem Trying to stop getting all of the collection, FireBase on Fire will throw me all of the Collection. Is it something to do with putting the Chat Class inside a Service? (Not an Intent Service). What I did is, I added the Chat Class where my Service Class is, and the FireBase Listeners are also in there. I will have to open up a Question about this, because I am not sure what might happen in the future.
      – Aizen
      Jul 29 '15 at 20:13










    • I can't say from your description. Post a question with a minimal-but-complete repro of the problem and I'll have a look. See stackoverflow.com/help/mcve
      – Frank van Puffelen
      Jul 29 '15 at 21:24






    • 1




      Incredibly enough somebody else today reached out with the same problem with an inner class. The reason is clear btw. A non-static inner class has an implicit reference to its containing class. And that's a field that Firebase cannot initialize for you when it deserializes. In other words: "bla bla bla... it must be static". :-)
      – Frank van Puffelen
      Jul 30 '15 at 0:18


















    • This is the Best answer Detailed and clear. From all the queries I have seen in FireBase Tag. The only thing missing here is the List Approach. Which I actually use from your example Class. BTW the Class getValue Example that I tried, is sending me an error Bounce Type. So what I did to fix it is, put Static on my Class. I HAVE NO IDEA WHY IT WORKED.
      – Aizen
      Jul 29 '15 at 19:36










    • Hmmm... that sounds interesting, I've never had to make the class static, but I never use inner classes for these anyway. Can you open a new question for that debouncing problem? Post the JSON, you Java class and the code how you read it.
      – Frank van Puffelen
      Jul 29 '15 at 20:00










    • Sure, I will add that question. For later. For Now I am having a problem Trying to stop getting all of the collection, FireBase on Fire will throw me all of the Collection. Is it something to do with putting the Chat Class inside a Service? (Not an Intent Service). What I did is, I added the Chat Class where my Service Class is, and the FireBase Listeners are also in there. I will have to open up a Question about this, because I am not sure what might happen in the future.
      – Aizen
      Jul 29 '15 at 20:13










    • I can't say from your description. Post a question with a minimal-but-complete repro of the problem and I'll have a look. See stackoverflow.com/help/mcve
      – Frank van Puffelen
      Jul 29 '15 at 21:24






    • 1




      Incredibly enough somebody else today reached out with the same problem with an inner class. The reason is clear btw. A non-static inner class has an implicit reference to its containing class. And that's a field that Firebase cannot initialize for you when it deserializes. In other words: "bla bla bla... it must be static". :-)
      – Frank van Puffelen
      Jul 30 '15 at 0:18
















    This is the Best answer Detailed and clear. From all the queries I have seen in FireBase Tag. The only thing missing here is the List Approach. Which I actually use from your example Class. BTW the Class getValue Example that I tried, is sending me an error Bounce Type. So what I did to fix it is, put Static on my Class. I HAVE NO IDEA WHY IT WORKED.
    – Aizen
    Jul 29 '15 at 19:36




    This is the Best answer Detailed and clear. From all the queries I have seen in FireBase Tag. The only thing missing here is the List Approach. Which I actually use from your example Class. BTW the Class getValue Example that I tried, is sending me an error Bounce Type. So what I did to fix it is, put Static on my Class. I HAVE NO IDEA WHY IT WORKED.
    – Aizen
    Jul 29 '15 at 19:36












    Hmmm... that sounds interesting, I've never had to make the class static, but I never use inner classes for these anyway. Can you open a new question for that debouncing problem? Post the JSON, you Java class and the code how you read it.
    – Frank van Puffelen
    Jul 29 '15 at 20:00




    Hmmm... that sounds interesting, I've never had to make the class static, but I never use inner classes for these anyway. Can you open a new question for that debouncing problem? Post the JSON, you Java class and the code how you read it.
    – Frank van Puffelen
    Jul 29 '15 at 20:00












    Sure, I will add that question. For later. For Now I am having a problem Trying to stop getting all of the collection, FireBase on Fire will throw me all of the Collection. Is it something to do with putting the Chat Class inside a Service? (Not an Intent Service). What I did is, I added the Chat Class where my Service Class is, and the FireBase Listeners are also in there. I will have to open up a Question about this, because I am not sure what might happen in the future.
    – Aizen
    Jul 29 '15 at 20:13




    Sure, I will add that question. For later. For Now I am having a problem Trying to stop getting all of the collection, FireBase on Fire will throw me all of the Collection. Is it something to do with putting the Chat Class inside a Service? (Not an Intent Service). What I did is, I added the Chat Class where my Service Class is, and the FireBase Listeners are also in there. I will have to open up a Question about this, because I am not sure what might happen in the future.
    – Aizen
    Jul 29 '15 at 20:13












    I can't say from your description. Post a question with a minimal-but-complete repro of the problem and I'll have a look. See stackoverflow.com/help/mcve
    – Frank van Puffelen
    Jul 29 '15 at 21:24




    I can't say from your description. Post a question with a minimal-but-complete repro of the problem and I'll have a look. See stackoverflow.com/help/mcve
    – Frank van Puffelen
    Jul 29 '15 at 21:24




    1




    1




    Incredibly enough somebody else today reached out with the same problem with an inner class. The reason is clear btw. A non-static inner class has an implicit reference to its containing class. And that's a field that Firebase cannot initialize for you when it deserializes. In other words: "bla bla bla... it must be static". :-)
    – Frank van Puffelen
    Jul 30 '15 at 0:18




    Incredibly enough somebody else today reached out with the same problem with an inner class. The reason is clear btw. A non-static inner class has an implicit reference to its containing class. And that's a field that Firebase cannot initialize for you when it deserializes. In other words: "bla bla bla... it must be static". :-)
    – Frank van Puffelen
    Jul 30 '15 at 0:18












    up vote
    1
    down vote













    The value will be returned as an Object, as you've stated, but you can actually cast it to Map<String, Object> since you know it should be a map.



    So, do something like this:



    Map<String, Object> val = (Map<String, Object>) dataSnapshot.getValue();
    String message = (String) val.get("message");
    String sender = (String) val.get("sender");





    share|improve this answer

























      up vote
      1
      down vote













      The value will be returned as an Object, as you've stated, but you can actually cast it to Map<String, Object> since you know it should be a map.



      So, do something like this:



      Map<String, Object> val = (Map<String, Object>) dataSnapshot.getValue();
      String message = (String) val.get("message");
      String sender = (String) val.get("sender");





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        The value will be returned as an Object, as you've stated, but you can actually cast it to Map<String, Object> since you know it should be a map.



        So, do something like this:



        Map<String, Object> val = (Map<String, Object>) dataSnapshot.getValue();
        String message = (String) val.get("message");
        String sender = (String) val.get("sender");





        share|improve this answer












        The value will be returned as an Object, as you've stated, but you can actually cast it to Map<String, Object> since you know it should be a map.



        So, do something like this:



        Map<String, Object> val = (Map<String, Object>) dataSnapshot.getValue();
        String message = (String) val.get("message");
        String sender = (String) val.get("sender");






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 29 '15 at 16:22









        nameless912

        351111




        351111






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31704859%2ffirebase-query-datasnapshot%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()