2014-11-04 66 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()