java - 有没有像我使用过的那样访问特定的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)
解决方案
您在 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));
}
}
}
...
推荐阅读
- objective-c - 在 Firebase 实时数据库上搜索/获取对象
- java - 我无法在我的表中或 phpmyadmin 上可视化列“特征”
- c# - 如何从 Windows Search 获取搜索结果片段
- mysql - MySQL Workbench - 单击时插入选项卡中的应用更改图标没有响应
- magento - 将 Magento2 404 页面重定向到注册页面
- powershell - 更改presentation.saveas pdf的选项
- python - 地址已在使用 (Errno 98) django 与 vscode 和 docker
- python - 为什么 np.where() 在数组切片的副本上比在原始数组上的视图上更快?
- scala - 使用 spark scala 根据 hive 查询结果添加列
- postgresql - Spark:java.sql.SQLException:找不到适合 jdbc:postgresql://localhost/postgres 的驱动程序