I'm developing a Recipes Book and I've a problem saving multiple Ingredients for the same Recipe in the database. I can add, pressing a button, multiples Linear Layout with inside an ingredient EditText and a quantity EditText. So the for cycle evaluates each Layout, it takes the currents ingredient and quantity values saving them in the newIngredient instance (of the Ingredient.class). Then it inserts the instance in the database and finally, it adds the instance to my "ingredients" ArrayList and closes the DB. With the debug, I found out that all this works only for the first iteration of for cycle.
for (int d=0; d<countIngredients; d++) {
View childViewIng = parentIngredientLayout.getChildAt(d);
EditText childTextViewI = childViewIng.findViewById(R.id.ingredientsField);
EditText childTextViewQ = childViewIng.findViewById(R.id.quantityField);
childIngredient = childTextViewI.getText().toString();
childQuantity = Integer.parseInt(childTextViewQ.getText().toString());
newIngredient = new Ingredient(childIngredient, childQuantity);
dbHelper.insertIngredient(newIngredient);
ingredients.add(newIngredient);
dbHelper.close();
}
dbHelper.close();outside for loop