在同一线程上打开/关闭SQL数据库

时间:2019-03-27 14:11:49

标签: android sql database

我正在开发一本食谱书,并且正在实现此方法以将我的食谱插入数据库中。在 for循环中,我从多个EditText获得了成分的名称和数量,并将它们分别保存在 Ingredient.class 实例( newIngredient )中。然后,将实例插入数据库,并将其添加到ArrayList。以下“如果条件” 用于标题,时间和其他配方属性。最后,我还将食谱和标签实例插入亲戚数据库的表中,然后关闭数据库。

public void saveRecipe() {

    dbHelper = new DatabaseHelper(context);

    // creating new recipe from user input
    Ingredient newIngredient;
    String title, childIngredient, instruction, tag;
    int target, time, childQuantity, calories;
    int countIngredients = parentIngredientLayout.getChildCount();
    int countTags = chipGroup.getChildCount();
    ArrayList<Ingredient> ingredients = null;
    ArrayList<Tag> tags = null;
    View childViewIng = null;
    EditText childTextViewI = null;
    EditText childTextViewQ = null;

    // ingredients fields settings
    for (int d=0; d<countIngredients; d++) {
        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);
    }

    //recipe fields settings
    if (photoPath1 == null)
        photoPath1 = "";

    if (photoPath2 == null)
        photoPath2 = "";

    if (photoPath3 == null)
        photoPath3 = "";

    if (titleText.getText().toString().isEmpty()) {
        title = "";
    } else {
        title = titleText.getText().toString();
    }

    if (targetNumber.getText().toString().isEmpty()) {
        target = 0;
    } else {
        target = Integer.parseInt(targetNumber.getText().toString());
    }

    if (timeNumber.getText().toString().isEmpty()) {
        time = 0;
    } else {
        time = Integer.parseInt(timeNumber.getText().toString());
    }

    if (instructionText.getText().toString().isEmpty()) {
        instruction = "";
    } else {
        instruction = instructionText.getText().toString();
    }

    if (caloriesNumber.getText().toString().isEmpty()) {
        calories = 0;
    } else {
        calories = Integer.parseInt(caloriesNumber.getText().toString());
    }

    if (tagName.getText().toString().isEmpty()) {
        tag = "";
    } else {
        tag = tagName.getText().toString();
    }

    Recipe newRecipe = new Recipe(title, photoPath1, photoPath2, photoPath3, instruction, target, time, calories, ingredients);
    Tag newTag = new Tag(tag);

    dbHelper.insertRecipe(newRecipe);
    dbHelper.insertTag(newTag);

    dbHelper.close(); }

我通过调试发现,在这种情况下,仅插入第一个成分。我尝试将FOR移至代码末尾,但在那种情况下,将同时插入配方和标签,并且始终只插入第一个成分。我认为问题与数据库的打开/关闭有关。有人可以帮我吗? 成分构造器:

public Ingredient(String ingredient_name, int quantity) {

    this.ingredient_name = ingredient_name;
    this.quantity = quantity;
}

dbHelper.insertIngredient(newIngredient)方法:

public boolean insertIngredient(Ingredient ingredient) {

    db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(INGREDIENT_NAME, ingredient.getIngredient_name());
    contentValues.put(QUANTITY, ingredient.getQuantity());
    contentValues.put(KEY_CREATED_AT, time.getTime().toString());

    long result = db.insert(TBL_INGREDIENTS, null, contentValues);
    //db.close();
    Log.e(TAG, "Ingredient inserted!");

    if (result == -1) {
        return false;
    } else {
        return true;
    }

}

1 个答案:

答案 0 :(得分:1)

好的,感谢您的评论,我们解决了问题:)

您正在用.add(newIngredient)初始化的列表上调用ArrayList<Ingredient> ingredients = null;

将其更改为

ArrayList<Ingredient> ingredients = new ArrayList<Ingredient>();

它将起作用:)

祝你好运!