java - 如何将增量发票编号放入DatabaseReference

标签 java android firebase firebase-realtime-database

我正在创建一张从 #1 #2 #3 .... 开始递增编号的发票,但我不知道如何开始。这是我想要的结构:

Invoice#1
|- info...
Invoice#2
|- info...
Invoice#3
|- info...
DatabaseReference ref 
 = FirebaseDatabase.getInstance().getReference().child("Invoice#" {increment}); //how to put increment here? if I declare i=1 in myActivity.java, it only runs in one

这是我的发票模型的一部分

public class Invoice {
    String buyer, seller, typeOfPymt;
    int total;
    String billdate;
    int invoice_num;
    int discount;
    int tax;
    int grand_total;


    public Invoice(String buyer, String seller, int grand_total, String billdate, int invoice_num, int discount) {
        this.buyer = buyer;
        this.seller = seller;
        this.billdate = billdate;
        this.invoice_num = invoice_num;
        this.discount = discount;
        this.grand_total = grand_total;
    }

如何在模型中添加增量?

最佳答案

您无法将“增量”参数传递给 child() 方法来帮助您读取最后一张发票号码并生成新的增量发票号码。

在您的数据库中,更合适的方法是使用 push() 生成的随 secret 钥作为发票的 key 。方法,而不是那些递增的语音号码。主要原因是可扩展性,@FrankvanPuffelen 在下面的文章中对此进行了解释:

使用上述方法,发票号码将成为发票对象中的属性。

关于java - 如何将增量发票编号放入DatabaseReference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58688615/

相关文章:

java - Maven 插件创建找不到错误

java - 无法使用 Java 获取 XML 标签

android - 创建可更新的 Android 应用程序

android - 从 Android 设备卸载应用程序时如何使用 Firebase 以编程方式检测?

android - 一个 Service Worker 进程停止另一个 Service Worker 进程,反之亦然

ruby-on-rails - 我正在使用 `fcm` gem 发送推送通知,它适用于 Android,但不适用于 IOS

java - 从 firebase android 检索图像的简单方法

Java - 根据多个值过滤列表

ios - 适用于 iOS 的 Xamarin Firebase 出现 fcm 501 错误

java - 有没有办法检测是否要在 JTree 上进行放置?