首页 > 解决方案 > 有没有像我使用过的那样访问特定的firebase子值?

问题描述

我有一个用户的firebase项目,孩子们作为“问题”和其他价值观。我通过将值设置为类对象来上传问题的孩子。

Question question = new Question(qID, uID, text, subject, imageLink, date, time, points, containsMedia);
FirebaseDatabase.getInstance().getReference()
    .child("Users")
    .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
    .child("Questions")
    .child(qID)
    .setValue(question);

我想将此子对象作为类对象访问。我想检索所有子节点的价值主题、时间和提供的所有信息。

数据库结构

我在我的主要活动中使用了这段代码:


    FirebaseDatabase.getInstance().getReference().child("Users").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                if (dataSnapshot.hasChild("Questions"))
                {

                        for (DataSnapshot d : dataSnapshot.getChildren())
                        {

                            String parent = d.getKey();

                            if (parent.equals("Questions"))
                            {
                                for (DataSnapshot question_object: d.getChildren())
                                {
                                    questions_list.add(question_object.getValue(Question.class));
                                }
                            }
                        }
                }

这是我的问题课。Questions.java

public class Question {
    public String qID, uID;
    public String text, subject, imageLink, date, time, points, containsMedia;

    public Question(String qID, String uID, String text, String subject, String imageLink, String date, String time, String points, String containsMedia) {
        this.qID = qID;
        this.uID = uID;
        this.text = text;
        this.subject = subject;
        this.imageLink = imageLink;
        this.date = date;
        this.time = time;
        this.points = points;
        this.containsMedia = containsMedia;
    }


    public String getqID() {
        return qID;
    }

    public String getuID() {
        return uID;
    }

    public String getText() {
        return text;
    }

    public String getSubject() {
        return subject;
    }

    public String getImageLink() {
        return imageLink;
    }

    public String getDate() {
        return date;
    }

    public String getTime() {
        return time;
    }

    public String getPoints() {
        return points;
    }

    public String getContainsMedia() {
        return containsMedia;
    }
}

这是 Logcat :


    com.google.firebase.database.DatabaseException: Class com.solvify.QA.Question does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@16.0.4:557)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@16.0.4:550)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.0.4:420)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.0.4:214)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.0.4:79)
        at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.0.4:212)
        at com.solvify.MainActivity$3.onChildAdded(MainActivity.java:138)
        at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:79)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:163)
        at android.app.ActivityThread.main(ActivityThread.java:6227)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
2020-04-21 11:42:22.823 369-2482/? E/ANDR-PERF-RESOURCEQS: Failed to apply optimization [4, 0]
2020-04-21 11:42:23.131 1470-1591/? E/NetdConnector: NDC Command {766 bandwidth removeiquota rmnet_data0} took too long (583ms)

标签: javaandroidfirebasefirebase-realtime-database

解决方案


您在 JSON 中迭代了一层太深。我总是发现通过在它们迭代的级别之后命名快照变量来防止这个错误是最容易的:

FirebaseDatabase.getInstance().getReference().child("Users").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot userSnapshot, @Nullable String s) {
            if (userSnapshot.hasChild("Questions")) {
                for (DataSnapshot questionSnapshot: userSnapshot.child("Questions").getChildren()) {
                     questions_list.add(questionSnapshot.getValue(Question.class));
                }
            }
        }
        ...

推荐阅读