Android Loop数据列表以上传到Firestore

时间:2019-03-29 14:10:35

标签: android google-cloud-firestore

我正在尝试将列表上传到我的Firestore,我已经进行了for循环,但是上传到Firestore的数据只是列表的最后一个值。因此,如果我在列表中输入3个项目,则只有添加到Firestore中的最后一个项目。为什么不能将所有列表值都添加到Firestore中?此循环的功能是更新物料的数量值,并将物料添加到销售单据中

我创建一个Alertdialog,供用户将项目添加到列表中

 addItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LayoutInflater li = CreateReceiptActivity.this.getLayoutInflater();

                View v = li.inflate(R.layout.alertdialog_create_receipt, null);
                AlertDialog.Builder builder = new AlertDialog.Builder(CreateReceiptActivity.this);
                builder.setView(v);

                final EditText addItemType = v.findViewById(R.id.alertdialog_receipt_type);
                final EditText addItemQty = v.findViewById(R.id.alertdialog_receipt_qty);
                final EditText addItemPrice = v.findViewById(R.id.alertdialog_receipt_price);
                Button btnSubmit = v.findViewById(R.id.alertdialog_receipt_submit);

                addItemType.setFilters(new InputFilter[]{new InputFilter.AllCaps()});


                final AlertDialog alertDialog = builder.show();
//
//                receiptItemList = new ArrayList<>();
//                listReceiptItem = new ListReceiptItem();
                btnSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        itemType = addItemType.getText().toString().trim();
                        itemQty = addItemQty.getText().toString().trim();
                        itemPrice = addItemPrice.getText().toString().trim();
                        listReceiptItem = new ListReceiptItem(itemType, itemQty, itemPrice,"0");
                        receiptItemList.add(listReceiptItem);
                        recyclerView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        alertDialog.dismiss();
                        Toast.makeText(CreateReceiptActivity.this, "barang tertambah", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

这是将数据添加到Firestore的方法

private void cutStock() {
        final FirebaseFirestore db = FirebaseFirestore.getInstance();

        for (int i = 0; i < receiptItemList.size(); i++) {
            final String soldItemDate = date.getText().toString().trim();
            final String soldItemInvoice = invoiceNumber.getText().toString().trim();
            final String soldItemtype = listReceiptItem.getType();
            final String soldItemQty = listReceiptItem.getQty();
            final String soldItemPrice = listReceiptItem.getPrice();

            db.collection("watchlist").whereEqualTo("type", soldItemtype)
                    .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Log.d(Tag.ITEM, document.getId() + "=>" + document.getData());

                            String id = document.getString("id");
                            String oldqty = document.getString("qty");
                            Integer i = Integer.parseInt(oldqty) - Integer.parseInt(soldItemQty);
                            String newQty = String.valueOf(i);

                            Map<Object, String> map = new HashMap<>();
                            map.put("qty", newQty);
                            db.collection("watchlist").document(document.getId()).set(map, SetOptions.merge());

                            Map<String, Object> receiptItem = new HashMap<>();
                            receiptItem.put("invoice", soldItemInvoice);
                            receiptItem.put("date", soldItemDate);
                            receiptItem.put("type", soldItemtype);
                            receiptItem.put("qty", soldItemQty);
                            receiptItem.put("price", soldItemPrice);


                            final FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
                            firebaseFirestore.collection("sales").add(receiptItem).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                                @Override
                                public void onSuccess(DocumentReference documentReference) {
                                    Toast.makeText(CreateReceiptActivity.this, "Berhasil mencetak transaksi", Toast.LENGTH_SHORT).show();
                                    Integer i = Integer.parseInt(soldItemInvoice) + 1;
                                    String newInvoice = String.valueOf(i);
                                    invoiceNumber.setText(newInvoice);

                                }
                            })
                                    .addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Toast.makeText(CreateReceiptActivity.this, "Gagal mencetak", Toast.LENGTH_SHORT).show();

                                        }
                                    });
                        }
                    } else {
                        Toast.makeText(CreateReceiptActivity.this, "Barang tidak terdaftar", Toast.LENGTH_SHORT).show();
                        Log.w(Tag.ITEM, "error getting documents", task.getException());
                    }
                }
            });
        }
    }

1 个答案:

答案 0 :(得分:0)

因此,您应该创建一个列表,添加所有对象,然后将该列表添加到firebaseFirestore。

类似这样的东西:

     List<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
     Map<String, Object> receiptItem = new HashMap<>();
                        receiptItem.put("invoice", soldItemInvoice);
                        list.add(receiptItem);
                        receiptItem.put("date", soldItemDate);
                        list.add(receiptItem);
                        receiptItem.put("type", soldItemtype);
                        list.add(receiptItem);
                        receiptItem.put("qty", soldItemQty);
                        list.add(receiptItem);
                        receiptItem.put("price", soldItemPrice);
                        list.add(receiptItem);