它应该很简单,但我无法弄清楚.. 我有这段代码:
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
答案 0 :(得分: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)
就像这样:
Form<?> form2 = new Form<Void>("form2") {
@Override
protected void onSubmit() {
...
DataView dataView = new DataView("listview", new ListDataProvider(list));
add(dataView);
...
}
};