2014-11-04 25 views
0

我正在寫一個視圖,當選擇一個表格條目(在右邊)時,導航到顯示在左側的表格條目頁面。這與Vaadin網站上的地址簿教程類似,只是我使用了導航器和視圖。Vaadin表如何與Navigator一起使用?

雖然我得到了導航工作(點擊ID爲12的條目導航到localhost:8080/test/12),並且視圖的enter()中的測試標籤被更改爲匹配id,testTable.getItem( event.getParameters())由於某種原因返回null,所以我無法訪問條目。

該視圖的ValueChangeListener和enter()如下所示。

class ValueChangeListener implements Property.ValueChangeListener { 
     Object testId; 

     @Override 
     public void valueChange(ValueChangeEvent event) { 
      // Navigate to a chosen table entry 
      this.testId = event.getProperty().getValue(); 
      navigator.navigateTo("test/" + testId); 
     } 
    } 

    ... 

    public void enter(ViewChangeEvent event) { 
     Object tmp = event.getParameters(); 
     testName.setValue((String) tmp); // is set to the id 
     System.out.println(testTable.getItem(tmp) == null); // DEBUG: always returns true 
    } 
+0

你如何創建'testTable'?你使用什麼類型的「容器」? – Krayo 2014-11-04 09:29:08

+0

@Krayo我爲表格使用了'IndexedContainer'。 'createTestTable()'將容器屬性添加到'ic',並用for循環填充它,該循環用'ic.addItem()'向表中添加對象並在返回'ic之前用'setValue()'設置該對象的值'。該表的創建與[Vaadin使用的地址簿教程](https://vaadin.com/tutorial/)類似。 – 2014-11-04 10:15:11

回答

0

我想你應該改變這樣的:

System.out.println(testTable.getItem(tmp) == null); 

這樣:

String str = (String) tmp; 
if (str != null && !str.isEmpty()) { 
    System.out.println(testTable.getItem(Integer.parseInt(str)) == null); 
} 
+0

我試過了,但是Eclipse給了我錯誤'java.lang.NumberFormatException:對於輸入字符串:「」'。測試表不是那麼大,並且parseLong給出相同的錯誤,所以它不能是超出最高整數的id的情況。 – 2014-11-04 12:22:38

+0

@MarkusM。我編輯了我的答案。第一次顯示異常時? – Krayo 2014-11-04 12:54:47

+0

'isEmpty()'不是根據Eclipse定義的對象,它的唯一建議是將'tmp'作爲'AbstractField '。 – 2014-11-04 17:49:36

0

我覺得有什麼不對,你如何管理你導航。 首先,當您使用導航器更改視圖時,您應該在「#」中添加適當的「URL片段」。 例如,Vaadin樣器使用: http://demo.vaadin.com/sampler/#foundation

如果您的網址有沒有 「#」 ViewChangeEvent.getParameters()爲您提供的isEmpty()

相關問題