我尝试将数组放入数据库。在 edittext 中,我写了例如大蒜、西红柿、洋葱,但是当我将数据保存到数据库并转到我的 php 数据库时,它只显示成分而不是该文本。如果我手动添加文本(我手动添加数字而不是成分,但它是一回事)到数据库,它看起来不错并创建了良好的 json 文件。我该如何解决这个问题?
主 Activity .java
String id;
ArrayList<String> ingredients = new ArrayList<String>();
@BindView(R.id.etId) EditText etId;
@BindView(R.id.etIngredients) EditText etIngredients;
id = etId.getText().toString();
ingredients = etIngredients.getText().toString();
食谱.java
public class Recipes {
String id;
List<String> ingredients;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<String> getIngredients() {
return ingredients;
}
public void setIngredients(List<String> ingredients) {
this.ingredients = ingredients;
}
}
以及如何检索列中的成分?
我用它从 json 文件中返回它,但它连续返回成分 [1,3,4,5,6]
Reipes rec = recipes.get(position);
holder.textViewId.setText(rec.getId());
holder.textViewIngredients.setText(rec.getIngredients().toString());
前 4 个食谱是手动添加的,而在 Android 应用程序中添加的配料。
最佳答案
你必须更换
ArrayList<String> ingredients = new ArrayList<String>();
通过
String ingredients;
并在你的食谱类中尝试改变成分的类型
String ingredients;
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
关于java - 如何将数组保存到 PHP 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46111971/