Firebase - Android. Subtract stock from a shopping cart

1

I'm developing a product store with shopping cart and blah blah, typical. However, I have a very strange drama hahaha. The drama has to do with the fact that, when buying a certain amount of products, it is obvious that the quantity purchased is subtracted from the total stock of the respective product. I have a piece of code that fulfills this function, but when executed, the total stock goes practically "to the B" hahaha, giving negative values, and begins to update the stock infinitely to the negative.

I charge the shopping cart through an adapter, it looks like this:

Now the database is the following: As you can see the stock is still being updated hahaha

Now the important thing, the code is this: The first thing I do is load all the products entered into the cart, in an ArrayList.

 //Se agregan a un ArrayList los elementos del carro de compras
    table_users.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            DataSnapshot productSnap = dataSnapshot.child("cartItems");
            Iterable<DataSnapshot> productsChild = productSnap.getChildren();

            for (DataSnapshot product : productsChild) {

                Product p = product.getValue(Product.class);
                products.add(p);
            }
        }

Then, in the OnClick of the buy button, I add a foreach to get the id of each product entered and access the respective child, and then set the product stock .

  //Recorrer arrayList para comprobar que se ingresan los productos
                            for(final Product p1 : products)
                            {

                                table_prods.child(p1.getIdProd()).addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        Product oldProduct = new Product();
                                        oldProduct = dataSnapshot.getValue(Product.class);

                                        int stockNuevo = 0;
                                        stockNuevo = Integer.parseInt(oldProduct.getStock()) - Integer.parseInt(p1.getStock());
                                        String stockNuevo2 = Integer.toString(stockNuevo);
                                        oldProduct.setStock(stockNuevo2);
                                        table_prods.child(p1.getIdProd()).setValue(oldProduct);



                                        products.clear();
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });

It should be noted that in the setStock () to make a test, I put any value, for example "50" and everything ok, changed all the stock corresponding to each product to 50. It must be a rookie error hahaha, but the truth I have no idea what it may be, that's why I go to the experts, I hope you can help me.

    
asked by Victor Rojas 08.12.2018 в 18:42
source

0 answers