13

我是 GWT 的新手。我正在编写一个简单的 GWT 程序,我需要在其中使用一个组合框,为此我使用了ValueListBox. 在那个组合中,我需要列出代表一年中月份的 1 到 12 的数字。但是该组合在最后附加null了价值。谁能帮助我如何删除该null值?

    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;
    }

在此处输入图像描述

4

2 回答 2

25

打电话setValue之前setAcceptableValues

原因是值是null在你调用的时候setAcceptableValuesValueListBox会自动将任意值(一般传给setValue)添加到可接受值列表中(这样该值就被实际设置好了,并且可以被用户选择,如果她重新选择了)选择了另一个值并想回到原来的值)。首先使用可接受值列表中的值调用setValue可以消除这种副作用。

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

于 2012-06-24T10:32:21.373 回答
2

引用这个问题

注意 setAcceptableValues 会自动将当前值(由 getValue 返回,默认为 null)添加到列表中(如果需要,setValue 也会自动将值添加到可接受值列表中)

因此,请尝试颠倒调用 setValue 和 setAcceptableValues 的顺序,如下所示:

monthCombo.setValue(1);
monthCombo.setAcceptableValues(getMonthList());
于 2012-06-24T10:32:25.260 回答