从 Firestore 增加价值

我的条目看起来像这样:

https://img1.sycdn.imooc.com/65b60fb00001b50110420595.jpg

在输入数据之前,必须使用条码扫描仪填写条码。当 Firestore 中存在条形码时,我希望增加产品数量。当条形码在Firestore中不可用时,Intent到另一个类。


代码 :


@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();

                    }

                }

            }

        }

    });

}


达令说
浏览 91回答 2
2回答

大话西游666

productQuantity要根据财产的存在增加财产的价值barCode,请尝试以下代码行:collectionReference.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onComplete(@NonNull Task<QuerySnapshot> task) {&nbsp; &nbsp; &nbsp; &nbsp; if (task.isSuccessful()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (QueryDocumentSnapshot document : task.getResult()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (document.getString("barCode") != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; collectionReference.document(document.getId()).update(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "productQuantity", FieldValue.increment(1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }});

ITMISS

这将确定条形码是否存在。您可以填写交易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        }      }    });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java