0
我通過json url與數據庫連接。我想添加與數據庫中的對象一樣多的按鈕。我必須動態地做。我找不到任何解決方案如何做到這一點。用json的文本動態創建按鈕
我通過json url與數據庫連接。我想添加與數據庫中的對象一樣多的按鈕。我必須動態地做。我找不到任何解決方案如何做到這一點。用json的文本動態創建按鈕
如果您想爲每個條目設置一個按鈕,您只需要獲取所有條目的請求,然後將JSON輸出反序列化爲您需要在項目中創建的對象類。例如,讓我們承認您在數據庫中的輸入是單個varchar。你做你的要求,如「SELECT * FROM表」,得到了JSON和反序列化像Entry.cs一類是你做,包括:
public string Text { get; set; }
然後,使用的foreach語句和一個名爲List,例如,條目,你將所有的結果作爲一個集合。然後,用這個列表,你做一個簡單的foreach,你創建一個按鈕:
foreach (Entry entry in entries) {
Button button = new Button(this)
button.SetText(entry.Text);
//Other button customizations, layoutparameters, etc
view.AddView(button); /*Where view is the parent element where you want your buttons
to be. Of course, you'll need a reference to it using FindViewById. It
can be a linear layout for example, so all your buttons will align.
}
而且應該是這樣。請注意,如果數據庫中有很多條目,可能會有很多按鈕,所以請考慮使用ScrollView。