android - 如何在 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);
}
解决方案
只需将上面的函数修改为:
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);
}
希望这可以帮助。
推荐阅读
- spring-boot - 如何使用 Spring Boot 对 Vault 使用 AppRole 身份验证?
- python-3.x - Keras TimeseriesGenerator:检查输入时出错
- php - Laravel 类别关系返回 null
- java - Java Retrofit 返回空值
- swift - iOS14 - ViewHierarchy 更改确实会阻止 UIView 扩展工作
- java - 在单个 API 中为多个存储库调用分页,JAVA JPA
- java - 更改 Android 中的日期格式获取 ParseException
- google-bigquery - 如何在新的 bigquery 项目中将表复制到空数据集?
- powershell - 如何在主机上正确使用powershell脚本?
- kubernetes - 配置内部 kubernetes keycloak 服务地址时,keycloak-nodejs-connect grantManager 无法验证令牌