如何在Codenameone的按钮特定操作后重新加载整个表单

3

当我从购物车中删除一个产品时,我希望刷新表单,我尝试了所有方法都没成功,有办法可以做到吗?

这是我的购物车类,它显示我购物车中的产品,当我从购物车中删除一个产品时,我想要刷新整个表单,但不知道怎么做,我几乎尝试了所有方法,但仍然无法完成。

public class Cart {
  Form f;

  public Cart() throws IOException {
      f = new Form("cart",BoxLayout.y());

      Button b = new Button("back");
      b.addActionListener(e->{
         AfficherProduits sp;
          try {
              sp = new AfficherProduits();
              sp.getF().show();
          } catch (IOException ex) {
              System.out.println("ERREUR DANS RETOUR LISTE PRODUITS ");
          }
      });
      f.add(b);

      //**********************************instanciation du panier********************************************************
      Panier panier=Panier.getInstance();


      //********************************Parcourir le panier**************************************************************
       ComponentGroup cg = new ComponentGroup();
      for (Lignedecommande c : panier.p)
      {
          Container c4 =new Container(BoxLayout.x());
          Container c3 =new Container(BoxLayout.y());
          Container c2=new Container(BoxLayout.x());
          Container c1=new Container(BoxLayout.y());
          Container c5=new Container(BoxLayout.y());

      //***************************les elements du containers************************************************************
          ImageViewer iv=new ImageViewer();
          iv.setImage(Image.createImage("/"+ c.getProduct().getImage()).scaled(80, 80));

          Button bt=new Button("X");

          bt.addActionListener(e->{
             panier.removeLine(c);
             //ShowListProduct sp=new ShowListProduct();
             //sp.getF().show();
             f.removeComponent(cg);
             f.refreshTheme();
          });
          //********************les boutons de modif quantite******************************************
          Button b1=new Button("+");
          Button b2=new Button("-");
          bt.getStyle().setPadding(0,0,0,0);

          //*****************************mettre le bouton X au milieu****************************************************
          Label lb1=new Label(".");
          Label lb2=new Label(".");
          lb1.setVisible(false);
          lb2.setVisible(false);
          c1.add(lb1);
          c1.add(bt);
          c1.add(lb2);

          c5.add(b1);
          c5.add(b2);

          c4.add(c1);
          c4.add(iv);

          c2.add(new Label(Integer.toString(c.getQuantite())));
          c2.add(c5);
          //c2.add(b2);

          //c3.add(new SpanLabel(c.getProduct().getName()));
          c3.add(c2);


          c4.add(c3);
          c4.add(new Label("$"+Float.toString(c.getProduct().getPrix()*c.getQuantite())));

          cg.add(c4);
      }
      f.add(cg);
  }

  public Form getF() {
      return f;
  }

  public void setF(Form f) {
      this.f = f;
  }
}

这是我查看购物车页面的方式。
  h = new AfficherProduits();
  h.getF().show();
1个回答

2

不要重新加载,而是重新创建和展示。

将所有代码从构造函数中移到名为createCartForm()的方法中。然后构造函数将执行:

f = createCartForm();

重新加载页面只需要执行以下操作:
setF(h.createCartForm());
getCurrentForm().setTransitionOut(CommonTransitions.createEmpty());
h.getF().show();

我用一个新实例替换了表单,并移除了默认的过渡效果,这样替换就不会有动画效果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接