0

它应该很简单,但我无法弄清楚..我有这个代码:

Form<?> form2 = new Form<Void>("form2") {
    @Override
    protected void onSubmit() {
        ...
    dataView = new DataView("listview", new ListDataProvider(list));
        ...
    }
}
add(dataView);

如何定义dataView以及在哪里?如果我尝试final DataView dataView = null;发生错误:The final local variable dataView cannot be assigned, since it is defined in an enclosing type

4

1 回答 1

0

您不能分配给最终变量,并且只能从封闭类型内部引用最终变量。但是,您可以从封闭类型内部改变最终变量。

如果DataView不是立即可用,您可能需要一个阻塞队列,该队列将阻塞直到可用:

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue; 

final BlockingQueue<DataView> dataViews = new ArrayBlockingQueue<DataView>();
Form<?> form2 = new Form<Void>("form2") {
   @Override
   protected void onSubmit() {
      ...
      dataViews.offer(new DataView("listview", new ListDataProvider(list)));
      ...
   }
};
//form is submitted in a different thread somewhere between here and add
...
add(dataViews.take());


现在不可能从你给我们的上下文中分辨出来,但最简单的解决方案,不需要任何最终变量,就是add(dataView)像这样在 submit 内部调用:

Form<?> form2 = new Form<Void>("form2") {
   @Override
   protected void onSubmit() {
      ...
      DataView dataView = new DataView("listview", new ListDataProvider(list));
      add(dataView); 
      ...
   }
};
于 2012-10-04T15:46:59.383 回答