gpt4 book ai didi

java - 从 Firestore 增加值(value)

转载 作者:行者123 更新时间:2023-12-02 01:15:31 33 4
gpt4 key购买 nike

我的条目如下所示:

1

在输入数据之前,必须使用条码扫描仪填写条码。当 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();
}
}
}
}
});
}

最佳答案

要根据 barCode 属性的存在来增加 productQuantity 属性的值,请尝试以下代码行:

collectionReference.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
if (document.getString("barCode") != null) {
collectionReference.document(document.getId()).update(
"productQuantity", FieldValue.increment(1)
);
}
}
}
}
});

关于java - 从 Firestore 增加值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58687174/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com