How to set values in Enum?

Asked

Viewed 338 times

0

Look at the Grid

inserir a descrição da imagem aqui

The record that is coming from the database is of an Enum class, the correct one was for the result in the grid to be printed name Value and not V.

This is the Enum;

  VALOR("V", "Valor"),
    PERCENTUAL("P", "Percentual"),
    DIA("D", "Dia"),
    HORA("H", "Hora");

I prefer to treat this Back-End problem rather than in Front-End, the Enum class is created as shown below because it will also be loaded in another part of the system as listbox, my idea would be when the value comes as V is automatically set as Value, when vier P was set automatically as Percentage and so on.

public enum EventoTipoMultiplicador implements EnumConverter<EventoTipoMultiplicador, String> {

    VALOR("V", "Valor"),
    PERCENTUAL("P", "Percentual"),
    DIA("D", "Dia"),
    HORA("H", "Hora");

    private final String codigo;
    private final String descricao;

    public static final EnumValues<EventoTipoMultiplicador, String> VALORES = EnumUtils
            .getEnumValuesMap(EventoTipoMultiplicador.class);

    EventoTipoMultiplicador(String codigo, String descricao) {
        this.codigo = codigo;
        this.descricao = descricao;
    }

    @Override
    @JsonValue
    public String getValue() {
        return codigo;
    }

    public String getDescricao() {
        return descricao;
    }



    @JsonCreator
    public static EventoTipoMultiplicador fromValue(String v) {
        return EventoTipoMultiplicador.VALORES.getEnum(v);
    }

}

These changes can be made here in this method below;

   @JsonCreator
    public static EventoTipoMultiplicador fromValue(String v) {
        return EventoTipoMultiplicador.VALORES.getEnum(v);
    }

When loading the GRID it displays the following result with the STS debug.

The problem is I don’t know how to change the method due to lack of experience.

inserir a descrição da imagem aqui

  • You just want to return the Enum description?

  • I need return the description and the code.

1 answer

1

return EventoTipoMultiplicador.VALORES.getEnum(v).getDescricao();

OR

Change the toString() of the enumerated to give return of the attribute descricao.

@Override
    public String toString() {
        return descricao;
    }

And when you want to get the description just do it enumerado.toString()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.