2

I've a firebase project of users with the childs as "Questions" and other values. I uploaded the childs of questions by setting value as a class object.

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

I want to access this child as a class object. I want to retrieve all the child nodes value the subject, the time and all the information provided.

Database structure

I've used this code in my main activity :


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

This is my Questions 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;
    }
}

Here's the 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)

3 Answers3

1

You're iterating one level too deep in your JSON. I always find it easiest to prevent this mistake by naming the snapshot variables after the level they iterate:

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));
                }
            }
        }
        ...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

addchildEvent is loop until you would not get all childs no extra for loop needed inside addchildEventListner , Use this code to get all childs of database,

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

            if (dataSnapshot.hasChild("Questions"))
            {
                questions_list.add(datasnapshot.getValue(Question.class));
            }
        }
0

Special Thanks to all who gave their suggestions. I re-read my Logcat carefully and fixed the problem. I added another constructor for my class with no parameters. It fixed my problem. Actually, this error was due to missing of a default constructor.

I simply added

 public Question()
    {

    }

It fixed the issue.