FireBase Query. DataSnapshot

后端 未结 2 974
青春惊慌失措
青春惊慌失措 2021-01-25 09:10

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

DataSnapshot { key = user-4, value = {-JvFuwKX7r7o0ThXc0x8={sender=unit_owner, messag         


        
相关标签:
2条回答
  • 2021-01-25 09:47

    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.

    0 讨论(0)
  • 2021-01-25 09:56

    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");
    
    0 讨论(0)
提交回复
热议问题