tccpg288
tccpg288

Reputation: 3352

Cannot Capture Value From Nested Firebase Database

I have a firebase database structured below:

enter image description here

Essentially, I am trying to capture the value of "answer." I am running the below code:

 public void onDataChange(DataSnapshot dataSnapshot) {
                        //should dump all of poll creators items in the following section of the current user
                        Log.v("DataSnap", dataSnapshot.toString());
                        Log.v("DataSnap", dataSnapshot.child(USER_ID_LABEL).getValue().toString());
                        ArrayList<String > mPollAnswers = new ArrayList<>();
                        for (DataSnapshot y : dataSnapshot.child(ANSWERS_LABEL).getChildren()){
                            Log.v("ANSWER", y.getValue().toString());
                        }

And unfortunately it is returning both "vote count" and "answer," when I strictly want the value of answer:

enter image description here

Upvotes: 0

Views: 73

Answers (1)

martinomburajr
martinomburajr

Reputation: 1265

If you have access to the top-level keys e.g L3l1..., "answers" and "1", firebase will return the resultant object in the form of a Hashmap<String, Object> where Object can be anything.

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    firebaseDatabase.getReference("<top-level-key>")
            .child("L3l1...")
            .child("answers")
            .child("1")
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    //Returns a hashmap with keys of type answer and vote_count
                    Object value = dataSnapshot.getValue(); /
                    Object o = ((HashMap) value).get("answer");
                    String s = "";
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
  1. A more elegant solution To Simplify things, create an Answer class and cast it to the datasnapshot.

    public class Answer {
        private int vote_count;
        private String answer;
    
        public Answer() {
        }
    
        public int getVote_count() {
            return vote_count;
        }
    
        public void setVote_count(int vote_count) {
            this.vote_count = vote_count;
        }
    
        public String getAnswer() {
            return answer;
        }
    
        public void setAnswer(String answer) {
            this.answer = answer;
        }
    }
    

In your Activity or wherever you call firebase:

//In your Activity or Fragment or wherever you call Firebase
        firebaseDatabase.getReference("<top-level-key>")
            .child("L13a")
            .child("answers")
            .child("1")
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    Answer value = dataSnapshot.getValue(Answer.class);
                    //value.getAnswer() = "rstrrs"
                    //value.getVote_Count() = 0
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

Upvotes: 2

Related Questions