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