Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在SQL数据库中插入多行_Android_Sql_Database_Class_Arraylist - Fatal编程技术网

Android 如何在SQL数据库中插入多行

Android 如何在SQL数据库中插入多行,android,sql,database,class,arraylist,Android,Sql,Database,Class,Arraylist,我正在开发一本食谱书,但在数据库中保存同一食谱的多种成分时遇到问题。我可以添加,按下一个按钮,将线性布局与一个成分EditText和一个数量EditText相乘。 因此for循环评估每个布局,它获取当前配料和数量值,并将其保存在component.class的newcomponent实例中。然后将实例插入数据库,最后将实例添加到my Components ArrayList并关闭数据库。通过调试,我发现所有这些只适用于for循环的第一次迭代 for (int d=0; d<countIng

我正在开发一本食谱书,但在数据库中保存同一食谱的多种成分时遇到问题。我可以添加,按下一个按钮,将线性布局与一个成分EditText和一个数量EditText相乘。 因此for循环评估每个布局,它获取当前配料和数量值,并将其保存在component.class的newcomponent实例中。然后将实例插入数据库,最后将实例添加到my Components ArrayList并关闭数据库。通过调试,我发现所有这些只适用于for循环的第一次迭代

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();
    }

在代码中,您正在for循环中关闭数据库

因此,您的代码将执行第一次迭代,然后关闭数据库,因此下一次迭代将由于关闭的数据库连接而失败

你应该移动你的dbHeloper.close;呼叫外部循环

在这种情况下,可以将变量移到for循环之外,以更好地使用内存。短裤:

步骤1:循环周期结束后关闭数据库

步骤2:优化变量


希望这有帮助

尝试移动dbHelper.close;外部为Loop是否有任何数据库已关闭异常?
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);
}
//move close method here, outside loop
dbHelper.close();
//move variables here, or wherever you want
View childViewIng = null;
EditText childTextViewI = null;
EditText childTextViewQ = null;

for (int d=0; d < countIngredients; d++) {
    //in this way you will create only 1 object, and reuse it every time
    childViewIng = parentIngredientLayout.getChildAt(d);
    childTextViewI = childViewIng.findViewById(R.id.ingredientsField);
    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();