表单控件不重新验证

时间:2017-07-05 03:00:46

标签: java codenameone

我使用2个按钮从网络服务获取热门地点和酒店列表,并显示在多个列表中。

热门地点的

按钮1

酒店

按钮2

当我点击按钮1时,它显示了所有请求的列表,但当我返回单击按钮2时,它会返回酒店列表加入,其中包含之前显示的热门地点列表。 / p>

我想当我点击按钮1时它应该返回热门地点列表,当我返回点击按钮2时,它应该返回酒店列表。 我试图重新验证表单,但没有运气。 这是我的代码:

protected void beforeFormA(Form f) {
        Button bn = new Button("Exit");
        Container c = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        switch (i) {
            case 1://for Button 1
                Style s = UIManager.getInstance().getComponentStyle("Button");
                FontImage p = FontImage.createMaterial(FontImage.MATERIAL_PORTRAIT, s);
                EncodedImage placeholder = EncodedImage.createFromImage(p.scaled(p.getWidth() * 3, p.getHeight() * 4), false);
                f.setTitle("Tourist Attractions");
                getattractive();
                ArrayList arr = (ArrayList) response.get("results");
                for (Object m : arr) {
                    Map ma = (Map) m;
                    address = (String) ma.get("formatted_address");
                    name = (String) ma.get("name");
                    icon = (String) ma.get("icon");
                    data.add(createListEntry(name, address, icon));
                }
                DefaultListModel<Map<String, Object>> model = new DefaultListModel<>(data);
                MultiList ml = new MultiList(model);
                ml.getUnselectedButton().setIconName("icon_URLImage");
                ml.getSelectedButton().setIconName("icon_URLImage");
                ml.getUnselectedButton().setIcon(placeholder);
                ml.getSelectedButton().setIcon(placeholder);
                findContainer(f).add(BorderLayout.CENTER, ml);
                c.addComponent(bn);
                bn.addActionListener((ActionEvent evt) -> {
                    System.exit(0);
                });
                findContainer(f).add(BorderLayout.SOUTH, c);
                f.getComponentForm().revalidate();
                break;

            case 2://for Button 2
                Style s1 = UIManager.getInstance().getComponentStyle("Button");
                FontImage p1 = FontImage.createMaterial(FontImage.MATERIAL_PORTRAIT, s1);
                EncodedImage placeholder1 = EncodedImage.createFromImage(p1.scaled(p1.getWidth() * 3, p1.getHeight() * 4), false);

                f.setTitle("Hotels");
                gethotels();
                ArrayList arr1 = (ArrayList) response.get("results");
                for (Object m : arr1) {
                    Map ma = (Map) m;
                    address = (String) ma.get("formatted_address");
                    name = (String) ma.get("name");
                    icon = (String) ma.get("icon");
                    data.add(createListEntry(name, address, icon));
                }
                DefaultListModel<Map<String, Object>> model1 = new DefaultListModel<>(data);
                MultiList ml1 = new MultiList(model1);
                ml1.getUnselectedButton().setIconName("icon_URLImage");
                ml1.getSelectedButton().setIconName("icon_URLImage");
                ml1.getUnselectedButton().setIcon(placeholder1);
                ml1.getSelectedButton().setIcon(placeholder1);
                findContainer1(f).add(BorderLayout.CENTER, ml1);
                c.addComponent(bn);
                bn.addActionListener((ActionEvent evt) -> {
                    System.exit(0);
                });
                findContainer1(f).add(BorderLayout.SOUTH, c);
                f.getComponentForm().revalidate();
                break;
        }
    }

1 个答案:

答案 0 :(得分:1)

您似乎永远不会清除data列表...所以之前的项目(data.add(createListEntry(name, address, icon));添加)仍然存在。

你应该在每个案件的开头清楚:

data.clear();