java - 如何将数组保存到 PHP 数据库中

标签 java php android json

我尝试将数组放入数据库。在 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());

my online database

前 4 个食谱是手动添加的,而在 Android 应用程序中添加的配料。

how it looks when get data in 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/

相关文章:

php - fatal error :调用未定义的方法 mysqli::error()

php - 如何将此 HTML 表格转换为 XML?

java - PDF 提取中缺少表格布局信息

java - com.fasterxml.jackson.databind.JsonMappingException : Can not deserialize instance of java. util.ArrayList 超出 VALUE_NUMBER_INT token

java - JPA self-join问题,我怎么只能得到第一级引用?

java - Android ListView 的过滤器 - 空格字符

java - 有没有办法在桌面应用程序中转换 Android 应用程序?

java - Apache POI 中的多个超链接

php - CodeIgniter $this->input->get() 不工作

android - 如何在 fragment 中创建选项菜单