javagwtcastingsmartgwt

Translate numeric value to it's corresponding label using cell formatter from LinkedHashMap


I'm trying to display the value from a LinkedHashMap by using the key on a column in a grid (PaginatedGrid).

In the grid, I try casting the value from public String format() into short.

In the grid, the following is the code I've tried

grid.getField(MerchandiseSellerTargetsDS.FLD_TARGETTYPE).setCellFormatter(new CellFormatter() {

            @Override
            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                short messageValue = ((Integer) value).shortValue();
                return Messages.targetTypeMap.get(messageValue);
            }
        }); 

In Messages.java

public static LinkedHashMap<Number, String> targetTypeMap;

targetTypeMap=new LinkedHashMap<>();
        targetTypeMap.put((short)1,"Value");
        targetTypeMap.put((short)2,"Unit");

I expect the column to be populated with the value from the map e.g. if the key is 1 "Value" is displayed and if 2 "Unit" is displayed. However, the column not populated (it appears blank when I execute). What could I have done wrong or is it how I cast?


Solution

  • Well, the problem was on how I was casting. Here I convert value to a String which I then convert to Short type

    grid.getField(MerchandiseSellerTargetsDS.FLD_TARGETTYPE).setCellFormatter(new CellFormatter() {         
        @Override
        public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
            short targetTypeMapKey = Short.valueOf((String)value);
            return Messages.targetTypeMap.get(targetTypeMapKey);
        }
        });