首页 > 解决方案 > 如何在 Firebase android 中添加多组数据而不覆盖它们

问题描述

我是 Firebase 的新手,我正在尝试创建一个库存系统。我正在尝试将项目添加到库存中。单击提交按钮后,它将调用此函数。我想要保存多个项目,但这里发生的是它不断覆盖现有项目。我希望我的数据库看起来像下图中的产品, 这张图片在这里 但我不希望标题是唯一的我希望标题是序列号。

 private void writeNewItem(String ProductName, String SerialNo, String Quantity, String SupplierName, String SupplierPhone, String SupplierMail) {
    mDatabase.child("Product Name").setValue(ProductName);
    mDatabase.child("Serial No").setValue(SerialNo);
    mDatabase.child("Quantity").setValue(Quantity);
    mDatabase.child("Supplier Name").setValue(SupplierName);
    mDatabase.child("Supplier Phone").setValue(SupplierPhone);
    mDatabase.child("Supplier Mail").setValue(SupplierMail);
}

标签: androidfirebasefirebase-realtime-database

解决方案


只需将上面的函数修改为:

private void writeNewItem(String ProductName, String SerialNo, String Quantity,String SupplierName, String SupplierPhone, String SupplierMail) {
    DatabaseReference newProductRef = mDatabase.push();
    newProductRef.child("Product Name").setValue(ProductName);
    newProductRef.child("Serial No").setValue(SerialNo);
    newProductRef.child("Quantity").setValue(Quantity);
    newProductRef.child("Supplier Name").setValue(SupplierName);
    newProductRef.child("Supplier Phone").setValue(SupplierPhone);
    newProductRef.child("Supplier Mail").setValue(SupplierMail);
}

希望这可以帮助。


推荐阅读