在SQL数据库的一行中插入ArrayList

时间:2019-03-29 09:28:48

标签: android sql database arraylist

在我的食谱日记中,我有3个课程,即Recipe.class:

public Recipe(String title, String firstImage, String secondImage, String thirdImage, String instructions, int targetPeople, int time, int calories, ArrayList<Ingredient> ingredients, ArrayList<Tag> tags) {

    this.title = title;
    this.firstImage = firstImage;
    this.secondImage = secondImage;
    this.thirdImage = thirdImage;
    this.instructions = instructions;
    this.targetPeople = targetPeople;
    this.time = time;
    this.calories = calories;
    this.ingredients = ingredients;
    this.tags = tags;
}

Ingredient.class:

public Ingredient(String ingredient_name, int quantity) {

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

和Tag.class:

public Tag(String tag_name) {

    this.tag_name = tag_name;

}

当我保存新食谱时,我使用两个周期来存储标签和配料,并将它们中的每一个添加到相应的ArrayList <>(将更多的配料和标签链接到同一食谱),如下所示:

for (int c=0; c < countTags; c++) {
           childChipView = (Chip) chipTagGroup.getChildAt(c);
           childTextViewTag = childChipView.findViewById(R.id.chip_item);
        childTag = childTextViewTag.getText().toString();
        newTag = new Tag(childTag);
        dbHelper.insertTag(newTag);
        tags.add(newTag);
    }

    // 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_TABLE,INGREDIENT_TABLE,TAG_TABLE)。我的问题是,如何在 RECIPE_TABLE 中存储两个ArrayLists?我想要在表格的同一行中显示标题,说明,时间,卡路里等,以及两个ArrayLists

1 个答案:

答案 0 :(得分:1)

您不应连续插入整个数组,因为这违反了1NF,请尝试将其拆分为2个表