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

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

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(&quot;Teachers&quot;).child(&quot;Accounts&quot;)
    .setValue(dataClass).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
        @Override
        public void onComplete(@NonNull Task&lt;Void&gt; task) {
            if (task.isSuccessful()) {
                Toast.makeText(AddSection.this, &quot;Section added successfully&quot;, 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" //&#128072; 新添加的。

然后,您需要创建一个指向"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: &quot;Joseph&quot;
                         |
                         --- pSectionName: &quot;BSIT201A&quot;
                         |
                         --- 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:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference secNameRef = db.child(&quot;Teachers/Accounts/0000-123455/Sections/BSIT201A&quot;);
Map&lt;String, Object&gt; update = new HashMap&lt;&gt;();
update.put(&quot;subject&quot;, &quot;Subject Name&quot;);
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:

确定