从Firebase数据库添加相同产品时,我需要更新数量值

时间:2019-06-25 14:57:25

标签: android firebase

Image of Database

以下是我添加新商品并需要更新数量而不是在添加同一商品时替换它的代码

subType = subTypeSpinner.getSelectedItem().toString();
                    size = sizeSpinner.getSelectedItem().toString();
                    quantity = Integer.parseInt(quantityEditText.getText().toString());
                    final ProductModal newIronModal = new ProductModal(subType,size,quantity);
                    newIronModal.setSubTypeId(subType + "_" + size);
                    ironReference.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            for(DataSnapshot ds : dataSnapshot.getChildren()){

                                Log.i("CheckPosition","Inside For");

                                if (ds.getKey().matches(newIronModal.getSubTypeId())){
                                    Log.i("CheckPosition","Inside If");
                                    ProductModal ironModal = ds.getValue(ProductModal.class);
                                    newIronModal.setQuantity(ironModal.getQuantity() + newIronModal.getQuantity());
                                    Log.i("CheckUpdate",String.valueOf(newIronModal.getQuantity()));

                                }
                            }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });
                    ironReference.child(newIronModal.getSubTypeId()).setValue(newIronModal);
                    spinnerLinearLayout.setVisibility(View.GONE);

0 个答案:

没有答案