英文:
How to insert to Realtime Database under the child?
问题
如何在Java中编写代码来创建像图像中的实例?我正在尝试创建它,但它只会插入到"Teacher"父项下面。
以下是我正在尝试编写的代码。
FirebaseDatabase.getInstance().getReference("Teachers").child("Accounts")
.setValue(dataClass).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(AddSection.this, "Section added successfully", Toast.LENGTH_SHORT).show();
finish();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(AddSection.this, e.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
});
}
英文:
How do I do the code in Java to create an instance like in the image? I am trying to create that but it only inserts under the Teacher parent.
Below is the code that I am trying to do.
FirebaseDatabase.getInstance().getReference("Teachers").child("Accounts")
.setValue(dataClass).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(AddSection.this, "Section added successfully", Toast.LENGTH_SHORT).show();
finish();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(AddSection.this, e.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
});
}
答案1
得分: 1
如果您想要在类似以下结构中添加新的子项:
db
|
--- 教师
|
--- 帐户
|
--- 0000-123455
|
--- 部分
|
--- BSIT201A
|
--- 学生
| |
| --- studentName: "Joseph"
|
--- pSectionName: "BSIT201A"
|
--- subject: "Subject Name" //👈 新添加的。
然后,您需要创建一个指向"BSIT201A"节点的引用,并调用[updateChildren()][1],就像以下代码行一样:
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference secNameRef = db.child("Teachers/Accounts/0000-123455/Sections/BSIT201A");
Map<String, Object> update = new HashMap<>();
update.put("subject", "Subject Name");
secNameRef.updateChildren(update);
我还建议您将一个完整的监听器附加到updateChildren()操作,以查看是否出现问题。
[1]: https://firebase.google.com/docs/reference/android/com/google/firebase/database/DatabaseReference#updateChildren(java.util.Map%3Cjava.lang.String,java.lang.Object%3E)
英文:
If you want to add a new child in a structure that looks like this:
db
|
--- Teachers
|
--- Accounts
|
--- 0000-123455
|
--- Sections
|
--- BSIT201A
|
--- Students
| |
| --- studentName: "Joseph"
|
--- pSectionName: "BSIT201A"
|
--- subject: "Subject Name" //👈 Newly added.
Then you have to create a reference that points to the BSIT201A
node and call updateChildren(), like in the following lines of code:
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference secNameRef = db.child("Teachers/Accounts/0000-123455/Sections/BSIT201A");
Map<String, Object> update = new HashMap<>();
update.put("subject", "Subject Name");
secNameRef.updateChildren(update);
I also recommend you attach a complete listener to the updateChildren() operation, to see if something goes wrong.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论