以下是我添加新商品并需要更新数量而不是在添加同一商品时替换它的代码
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);