0

如果值列表只有一个项目,我想通过代码选择组合框的第一项。我试过这个:

Comboitem item = new Comboitem();
for(Empresa e : empresasList){              
    item.setValue(e.getEmpId());
    item.setLabel(e.getEmpNombre());
    item.setParent(cb_empresa);
}
if(empresasList.size()==1){             
    idEmpresa = empresasList.get(0).getEmpId();
//cb_empresa.setSelectedIndex(0);
    cb_empresa.setSelectedItem(item);
}

但它不起作用。我还尝试了注释行:

//cb_empresa.setSelectedIndex(0);

有什么帮助吗?

谢谢!

4

3 回答 3

0

首先,您的代码有问题。您必须创建与 empresasList.size() 一样多的 Comboitems。我想像

for(Empresa e : empresasList){
    Comboitem item = new Comboitem();              
    item.setValue(e.getEmpId());
    item.setLabel(e.getEmpNombre());
    cb_empresa.appendItem(item);
}
if(cb_empresa.getItemCount()==1){             
    cb_empresa.setSelectedIndex(0);
}

应该管用。然而,正如Aloong 所提到的,ZK 中有一个错误,即 setSelectedIndex() 必须推迟。据我记得,这个错误已被修复。如果没有,您可以使用 Event.echoEvent() 作为解决方法。

于 2011-10-12T08:22:47.853 回答
0

我不知道这是否item.setParent(cb_empresa);对视图有立即影响。您可以稍后尝试设置索引,或者在刷新 cb_empresa 之后尝试设置索引。

于 2011-09-28T09:37:01.277 回答
0
for(Empresa e : empresasList){
    Comboitem item = new Comboitem();              
    item.setValue(e.getEmpId());
    item.setLabel(e.getEmpNombre());
    cb_empresa.appendChild(item);
}
if(cb_empresa.getItemCount()==1){             
    cb_empresa.setSelectedIndex(0);
}
于 2014-12-05T12:46:21.563 回答