0
I have a selectOneMenu
thus:
<p:selectOneMenu id="diminui" value="#{naturemb.nature.diminui}" effect="clip">
<f:selectItems itemLabel="#{naturemb.carregarAtributos()}" itemValue="#{naturemb.carregarAtributos()}" />
</p:selectOneMenu>
I want to display on the screen the values of my Enum:
package br.com.pokemax.modelo;
public enum Atributos {
HP("HP"), ATTACK("Attack"), DEFENSE("Defense"), SPECIAL_ATTACK("Sp. Attack"), SPECIAL_DEFENSE("Sp. Defense"), SPEED(
"Speed");
private String nome;
private Atributos(String nome) {
this.nome = nome;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
I created a method in Controller like this:
public String carregarAtributos() {
List<Atributos> lista = Arrays.asList(Atributos.values());
for (int i = 0; i < lista.size(); i++) {
return lista.get(i).getNome();
}
return "";
}
But it’s not working, someone can help me ?
Here’s a hint. Use theEnhanced-for:
for (Atributos a : Atributos.values()) { return lista.getNome(); }
– Victor Stafusa
@Victorstafusa what the difference and advantage?
– Roknauta
Douglas, with that you don’t need to be wearing one
int
all the time to index and find the element, thus making the code much simpler. In addition, for some list types (linked lists), the methodget(int)
is relatively slow, but with this form of iteration, you avoid it.– Victor Stafusa