I'm having an issue while gathering data from a node on Firebase. I got a variable named stockBuy
, I have run my app on debug mode and it gets the value correctly but out of onDataChange()
my variable becomes to be 1 instead of the value from firebase + 1 as I have on this line stockBuy = stockBuy + 1;
Here you can see my code
private void deleteProduct(final int position){
mDataBase3 = FirebaseDatabase.getInstance().getReference().child("Products");
mDataBase4 = FirebaseDatabase.getInstance().getReference().child("Wishes");
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Products").child(MainActivity.carrito.get(position).getId());
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
stockBuy = Integer.parseInt(dataSnapshot.child("stock").getValue().toString());
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
stockBuy = stockBuy + 1;
mDataBase3.child(MainActivity.carrito.get(position).getId()).child("stock").setValue(stockBuy);
mDataBase4.child(userUid).child(MainActivity.carrito.get(position).getId()).child("stock").setValue(stockBuy);
MainActivity.carrito.remove(position);
this.cAdapter.notifyDataSetChanged();
}