如何在子节点下插入到实时数据库?

huangapple go评论99阅读模式
英文:

How to insert to Realtime Database under the child?

问题

如何在Java中编写代码来创建像图像中的实例?我正在尝试创建它,但它只会插入到"Teacher"父项下面。

以下是我正在尝试编写的代码。

  1. FirebaseDatabase.getInstance().getReference("Teachers").child("Accounts")
  2. .setValue(dataClass).addOnCompleteListener(new OnCompleteListener<Void>() {
  3. @Override
  4. public void onComplete(@NonNull Task<Void> task) {
  5. if (task.isSuccessful()) {
  6. Toast.makeText(AddSection.this, "Section added successfully", Toast.LENGTH_SHORT).show();
  7. finish();
  8. }
  9. }
  10. }).addOnFailureListener(new OnFailureListener() {
  11. @Override
  12. public void onFailure(@NonNull Exception e) {
  13. Toast.makeText(AddSection.this, e.getMessage().toString(), Toast.LENGTH_SHORT).show();
  14. }
  15. });
  16. }
英文:

如何在子节点下插入到实时数据库?

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.

  1. FirebaseDatabase.getInstance().getReference(&quot;Teachers&quot;).child(&quot;Accounts&quot;)
  2. .setValue(dataClass).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
  3. @Override
  4. public void onComplete(@NonNull Task&lt;Void&gt; task) {
  5. if (task.isSuccessful()) {
  6. Toast.makeText(AddSection.this, &quot;Section added successfully&quot;, Toast.LENGTH_SHORT).show();
  7. finish();
  8. }
  9. }
  10. }).addOnFailureListener(new OnFailureListener() {
  11. @Override
  12. public void onFailure(@NonNull Exception e) {
  13. Toast.makeText(AddSection.this, e.getMessage().toString(), Toast.LENGTH_SHORT).show();
  14. }
  15. });
  16. }

答案1

得分: 1

  1. 如果您想要在类似以下结构中添加新的子项:
  2. db
  3. |
  4. --- 教师
  5. |
  6. --- 帐户
  7. |
  8. --- 0000-123455
  9. |
  10. --- 部分
  11. |
  12. --- BSIT201A
  13. |
  14. --- 学生
  15. | |
  16. | --- studentName: "Joseph"
  17. |
  18. --- pSectionName: "BSIT201A"
  19. |
  20. --- subject: "Subject Name" //&#128072; 新添加的。
  21. 然后,您需要创建一个指向"BSIT201A"节点的引用,并调用[updateChildren()][1],就像以下代码行一样:
  22. DatabaseReference db = FirebaseDatabase.getInstance().getReference();
  23. DatabaseReference secNameRef = db.child("Teachers/Accounts/0000-123455/Sections/BSIT201A");
  24. Map<String, Object> update = new HashMap<>();
  25. update.put("subject", "Subject Name");
  26. secNameRef.updateChildren(update);
  27. 我还建议您将一个完整的监听器附加到updateChildren()操作,以查看是否出现问题。
  28. [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:

  1. db
  2. |
  3. --- Teachers
  4. |
  5. --- Accounts
  6. |
  7. --- 0000-123455
  8. |
  9. --- Sections
  10. |
  11. --- BSIT201A
  12. |
  13. --- Students
  14. | |
  15. | --- studentName: &quot;Joseph&quot;
  16. |
  17. --- pSectionName: &quot;BSIT201A&quot;
  18. |
  19. --- subject: &quot;Subject Name&quot; //&#128072; 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:

  1. DatabaseReference db = FirebaseDatabase.getInstance().getReference();
  2. DatabaseReference secNameRef = db.child(&quot;Teachers/Accounts/0000-123455/Sections/BSIT201A&quot;);
  3. Map&lt;String, Object&gt; update = new HashMap&lt;&gt;();
  4. update.put(&quot;subject&quot;, &quot;Subject Name&quot;);
  5. secNameRef.updateChildren(update);

I also recommend you attach a complete listener to the updateChildren() operation, to see if something goes wrong.

huangapple
  • 本文由 发表于 2023年2月24日 15:01:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定