代码之家  ›  专栏  ›  技术社区  ›  gooamoko

zk radiogroup返回错误的getSelectedIndex()。

  •  0
  • gooamoko  · 技术社区  · 6 年前

    我使用的是zk 8.5.2.1,我有一个弹出窗口 Radiogroup . 这个 zul :

    <zk xmlns="http://www.zkoss.org/2005/zul" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">
        <popup use="com.myproject.webapp.docbrowse.filter.FiltrationModePopup" width="330px">
                <vlayout sclass="content">
                    <hlayout>
                        <label sclass="title" value="List form setup"/>
                    </hlayout>
                    <radiogroup id="rgScrollerMode" sclass="scrollermode-radio">
                        <grid>
                            <rows>
                                <row sclass="radio-border">
                                    <radio label="Standart"/>
                                </row>
                                <row sclass="radio-border">
                                    <radio label="Archive / Select year"/>
                                    <hlayout id="archivePanel" use="com.myproject.webapp.docbrowse.ArchivePanel"/>
                                </row>
                                <row sclass="radio-border">
                                    <radio label="Trash"/>
                                </row>
                            </rows>
                        </grid>
                    </radiogroup>
            . . .
                    <div align="right">
                        <button id="okButton" label="Accept" sclass="acceptButton"/>
                        <button id="cancelButton" label="Cancel" sclass="cancelButton"/>
                    </div>
                </vlayout>
        </popup>
    </zk>
    

    等级 FiltrationModePopup :

    public class FiltrationModePopup extends Popup implements AfterCompose, IdSpace {
        public static final String STANDARD_MODE = "/icons/toolbar/table_mode.png";
        private static final String ARCHIVE_MODE = "/icons/toolbar/archive_mode.png";
        private static final String RECYCLEBIN_MODE = "/icons/toolbar/recycle_mode.png";
        private Radiogroup rgScrollerMode;
        //. . . other properties
    
        @Override
        public void afterCompose() {
            . . .
            rgScrollerMode = (Radiogroup) getFellow("rgScrollerMode");
            final Button okButton = (Button) getFellow("okButton");
            okButton.addEventListener(Events.ON_CLICK, new SerializableEventListener<Event>() {
                @Override
                public void onEvent(Event event) {
                    filtrationModeSaver.save(FiltrationModePopup.this);
                    final FiltrationSettings filtrationSettings = createFiltrationSettingsFromPopup();
                    EventUtils.postEvent(new FiltersChangeEvent(FiltrationModePopup.this, filtrationSettings));
                    EventUtils.postEvent(new RefreshScrollerEvent(FiltrationModePopup.this));
    
                    close();
                }
            });
            addForward(Events.ON_OK, okButton, Events.ON_CLICK);
            EventUtils.registerHandler(this);
        }
    
        private FiltrationSettings createFiltrationSettingsFromPopup() {
            return new FiltrationSettings(getScrollerMode(), getArchiveValue());
        }
    
        public ScrollerMode getScrollerMode() {
            switch (rgScrollerMode.getSelectedIndex()) {
                case 1:
                    return ScrollerMode.ARCHIVE;
                case 2:
                    return ScrollerMode.RECYCLEBIN;
                default:
                    return ScrollerMode.STANDARD;
            }
        }
        // . . . Other code
    }
    

    所以,当弹出窗口显示在屏幕上,我选择了一些单选项并按下“接受”按钮时,选择的索引并不总是正确的。有时是-1,有时是旧的选择值,有时是正确的。发生了什么?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Hawk    6 年前

    我简化了您的代码,它总是得到正确的所选索引。

        public class FiltrationModePopup extends Popup implements AfterCompose, IdSpace {
            private Radiogroup rgScrollerMode;
    
            @Override
            public void afterCompose() {
                rgScrollerMode = (Radiogroup) getFellow("rgScrollerMode");
                final Button okButton = (Button) getFellow("okButton");
                okButton.addEventListener(Events.ON_CLICK, new SerializableEventListener<Event>() {
                    @Override
                    public void onEvent(Event event) {
                        System.out.println(rgScrollerMode.getSelectedIndex());
                        close();
                    }
                });
                addForward(Events.ON_OK, okButton, Events.ON_CLICK);
            }
        }
    

    你需要在打电话之前追踪发生的事情 getSelectedIndex() . 您可以在onclick监听器中注释代码,并逐步逐行添加代码,以找出根本原因。