有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Firestore的java增量值

我有这样的条目:

1

在输入数据之前,必须使用条形码扫描仪填写条形码。当Firestore中存在条形码时,我希望增加产品数量。当Firestore中没有条形码时,请尝试另一个类别

代码:

@Override
public void handleResult(Result result) {
    final String scanResult = result.getText();

    ToneGenerator toneNotification = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
    toneNotification.startTone(ToneGenerator.TONE_PROP_BEEP, 150);

    collectionReference.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot queryDocumentSnapshot : task.getResult()) {
                    // if barcode is existing, then product quantity to increment
                    if (queryDocumentSnapshot.getString("barCode") != null) {
                        collectionReference.document(queryDocumentSnapshot.getId()).update("productQuantity", FieldValue.increment(1));
                        Intent moveView = new Intent(ScannersActivity.this, ViewData.class);
                        moveView.putExtra("documentID", documentID);
                        startActivity(moveView);
                        finish();
                    } else { // If barcode is not available in firestore, then intent to another class
                        Intent moveCode = new Intent(ScannersActivity.this, AddItems.class);
                        moveCode.putExtra("sendDocumentID", documentID);
                        moveCode.putExtra("ScanResult", scanResult);
                        startActivity(moveCode);
                        finish();
                    }
                }
            }
        }
    });
}

共 (1) 个答案

  1. # 1 楼答案

    这将确定条形码是否存在。可以用Transaction填充注释的TODO来更新数量值

    public void handleResult(Result result) {
      String scanResult = result.getText();
      // ...
    
      collectionReference
        .whereEqualTo("barCode", scanResult)
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
          @Override
          public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful() && task.getResult().size() > 0) {
              // TODO barcode exists; increase qty
            }
            else {
              // barcode not found, new Intent
            }
          }
        });
    }