2017-09-08 67 views
2

我嘗試將數組放入數據庫中。在編輯文本中,我編寫例如大蒜,西紅柿,洋蔥,但是當我將數據保存到數據庫並轉到我的php數據庫時,而不是該文本時,它只會說成分。如果我手動添加文本(我手動添加數字而不是成分,但它是相同的)數據庫它看起來不錯,並創建良好的JSON文件。我怎樣才能解決這個問題?如何將數組保存到PHP數據庫中

MainActivity.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(); 

Recipes.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

+0

類型成分的開始爲什麼你是否將逗號分隔的值保存到單個字段中?這是一對多關係和外鍵的用途。開始正確地規範你的數據庫。然後,如果您說PHP仍未正確保存,那麼我們需要準確知道您從Java發送到PHP的數據字符串的格式。如果你很高興字符串是正確的,那麼我們需要看看正在執行保存的PHP代碼。 – ADyson

+0

其次,如果您從PHP服務器以JSON形式返回一組成分標識,並且您希望以不同方式顯示它們,則必須遍歷它們並單獨處理它們,而不是僅將數據作爲字符串轉儲到你的屏幕(這是你現在正在做的)。 – ADyson

+0

你可以簡單地發送你的成分作爲一個可變串,爲什麼把它作爲ArrayList

回答

0

你將不得不更換

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

通過

String ingredients; 

,並在您recipies類嘗試改變

String ingredients; 
public String getIngredients() { 
    return ingredients; 
} 

public void setIngredients(String ingredients) { 
    this.ingredients = ingredients; 
} 
相關問題