2012-06-24 73 views
13

我是GWT新手。我正在編寫一個簡單的GWT程序,我需要使用一個組合框,爲此我使用了一個ValueListBox的實例。在這個組合中,我需要列出1到12的數字,代表一年中的月份。但組合最後附加值爲null。任何人都可以請幫助我如何刪除該值null值?如何從ValueListBox值中刪除空值

final ValueListBox<Integer> monthCombo = new ValueListBox<Integer>(new Renderer<Integer>() { 

      @Override 
      public String render(Integer object) { 
       return String.valueOf(object); 
      } 

      @Override 
      public void render(Integer object, Appendable appendable) throws IOException { 
       if (object != null) { 

        String value = render(object); 
        appendable.append(value); 
       } 
      } 
     }); 
    monthCombo.setAcceptableValues(getMonthList()); 
    monthCombo.setValue(1); 

    private List<Integer> getMonthList() { 
     List<Integer> list = new ArrayList<Integer>(); 

     for (int i = 1; i <= 12; i++) { 
      list.add(i); 
     } 

     return list; 
    } 

enter image description here

回答

24

setAcceptableValues之前調用setValue

的原因是,當調用setAcceptableValues值爲null,並ValueListBox自動添加的任何值(通常傳遞到setValue)到可接受的值的列表(使得值實際上設置是,並且可以被選擇由用戶選擇,並且如果她選擇了另一個值並且想要回到原來的值則重新選擇)。首先調用setValue,其值將在可接受值列表中,否定此副作用。

http://code.google.com/p/google-web-toolkit/issues/detail?id=5477

+1

我剛引述你的答案在以前類似的問題,哈哈:) –

+3

謝謝托馬斯Broyer。有效。 –

+3

我試過了,它不工作。我仍然看到空......它真的感覺像一個錯誤,而不是一個功能。我正在運行2.5-rc1 –

2

從這個question報價:

謹防setAcceptableValues自動將當前值 (GetValue返回,並默認爲null)的列表(和setValue方法 自動添加值,如果需要 ,也可接受的值列表)

所以嘗試反演在你打電話的setValue和setAcceptableValues如下順序:

monthCombo.setValue(1); 
monthCombo.setAcceptableValues(getMonthList()); 
+0

非常感謝Adel Boutros。現在它運行良好... –