无法从嵌套的Firebase数据库中捕获值

时间:2018-01-21 04:22:20

标签: java android firebase firebase-realtime-database

我的firebase数据库结构如下:

enter image description here

基本上,我试图捕捉"回答的价值。"我正在运行以下代码:

 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());
                        }

不幸的是,它正在返回&#34;投票数&#34;并且&#34;回答,&#34;当我严格要求答案的价值时:

enter image description here

1 个答案:

答案 0 :(得分:2)

如果您可以访问顶级键,例如L3l1 ......,&#34;答案&#34;和&#34; 1&#34;,firebase将以Hashmap<String, Object>的形式返回结果对象,其中Object可以是任何内容。

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. 更优雅的解决方案 要简化事物,请创建一个Answer类并将其强制转换为数据快照。

    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;
        }
    }
    
  2. 在您的活动中或您调用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) {
    
                    }
                });