![](/img/trans.png)
[英]How to get specific data of child node by searching from firebase database in android studio - java?
[英]how to get child of child of child data in firebase android
我怎样才能得到这些数据?
我在键值方面遇到问题,因为这是推键,这是我首先添加的一些保存数据的代码。
创建表
reference = FirebaseDatabase.getInstance().getReference("COURSE");
String courseId = reference.push().getKey();
if (courseId != null) {
CourseTObject object = new CourseTObject();
object.setcId(courseId);
object.settId(firebaseUser.getUid());
object.setInstitute(instituteName);
object.setCourse(courseName);
if (section.equals("")){
object.setSection(null);
}else {
object.setSection(section);
}
reference.child(courseId).setValue(object);
}
添加一些数据
reference = reference.child(nodeId).child(quizTitleString);
reference.child("totalQuestions").setValue(questions);
reference.child("timeLimit").setValue(timer);
reference.child("eachQuestionMarks").setValue(marks);
reference.child("quizTitle").setValue(quizTitleString);
然后在下面提到一些子数据
reference = FirebaseDatabase.getInstance().getReference("COURSE");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
CourseTObject courseObject = dataSnapshot.getValue(CourseTObject.class);
assert courseObject != null;
if (courseObject.gettId().equals(firebaseUser.getUid())) {
if (courseObject.getInstitute().equals(institute)){
if (courseObject.getCourse().equals(course)){
if (courseObject.getSection().equals(section)){
String nodeId = dataSnapshot.getKey();
assert nodeId != null;
reference = reference.child(nodeId).child(quizTitleString).child("QUESTION"+questionNo);
reference.child("question").setValue(q);
reference.child("choice1").setValue(c1);
reference.child("choice2").setValue(c2);
reference.child("choice3").setValue(c3);
reference.child("choice4").setValue(c4);
reference.child("answer"+answerNo).setValue(answer);
}
}
}
}
}
}
model class CourseTObject 只有一些 getter setter exclude child of child nodes data
public class CourseTObject {
private String cId;
private String tId;
private String institute;
private String course;
private String section;
private String totalQuestions;
private String timeLimit;
private String eachQuestionMarks;
public CourseTObject() {
}
public CourseTObject(String cId, String tId, String institute, String course, String section, String totalQuestions, String timeLimit, String eachQuestionMarks) {
this.cId = cId;
this.tId = tId;
this.institute = institute;
this.course = course;
this.section = section;
this.totalQuestions = totalQuestions;
this.timeLimit = timeLimit;
this.eachQuestionMarks = eachQuestionMarks;
}
public String getcId() {
return cId;
}
public void setcId(String cId) {
this.cId = cId;
}
public String gettId() {
return tId;
}
public void settId(String tId) {
this.tId = tId;
}
public String getInstitute() {
return institute;
}
public void setInstitute(String institute) {
this.institute = institute;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public String getTotalQuestions() {
return totalQuestions;
}
public void setTotalQuestions(String totalQuestions) {
this.totalQuestions = totalQuestions;
}
public String getTimeLimit() {
return timeLimit;
}
public void setTimeLimit(String timeLimit) {
this.timeLimit = timeLimit;
}
public String getEachQuestionMarks() {
return eachQuestionMarks;
}
public void setEachQuestionMarks(String eachQuestionMarks) {
this.eachQuestionMarks = eachQuestionMarks;
}
}
如果我们看这段代码:
reference = FirebaseDatabase.getInstance().getReference("COURSE");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
CourseTObject courseObject = dataSnapshot.getValue(CourseTObject.class);
...
因此,您在数据库中的COURSE
处加载所有数据,然后遍历直接子节点(因此-N9...
在您的屏幕截图中),然后尝试从中读取CourseTObject
。
但是,如果我们查看您的数据库的屏幕截图,则该直接子节点中没有cId
、 tId
、 institude
和类似属性。 这些属性在QUIZ_1
下低一级。
事情应该会变得更好,你添加第二个循环:
reference = FirebaseDatabase.getInstance().getReference("COURSE");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot pushkeySnapshot : snapshot.getChildren()) {
for (DataSnapshot quiz1Snapshot : snapshot.getChildren()) {
CourseTObject courseObject = quiz1Snapshot.getValue(CourseTObject.class);
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.