1
我正在與Vaadin合作,我在遍歷ComboBox中的選項時遇到了一些麻煩。我有我的目標看起來像:如何遍歷Vaadin中的ComboBox?
class MyObject{
private String text;
private Integer i;
public MyObject(String text,Integer i){
this.text = text;
this.i = i;
}
public String toString(){
return text;
}
//Getters and setters omitted
}
我把它添加到框這樣的:
MyObject o1 = new MyObject("o1",23);
MyObject o2 = new MyObject("o2",44);
ComboBox box=new ComboBox();
box.addItem(o1);
box.addItem(o2);
這個偉大的工程,當我想選擇的數據:
MyObject o3 = (MyObject)box.getValue();
但現在我需要遍歷ComboBox中的選項,我不知道如何。我似乎需要某種ID,但我不知道如何使用它。我嘗試沒有成功以下,但它不工作(而實在是太醜了):
Collection IDs = box.getItemIds();
Iterator it = IDs.iterator();
while(it.hasNext()){
Object id = it.next();
Item item = IDs.getItem(id);
//What to do now?
}
我想保持我的目標簡單,避免使用豆類和複雜的容器。 Vaadins的例子主要用於String,這對我沒有太大的幫助。我非常感謝任何幫助。