Codenameone 代码名一回命令更新表单数据

Codenameone 代码名一回命令更新表单数据,codenameone,back,Codenameone,Back,我有一个Android应用程序,它使用Back命令返回开始屏幕 开始屏幕有一个标签,里面有一个数字,我想在使用back命令时更新这个标签 我可以用back命令中的代码找到一个解决方案,但我不知道我的方法是否是最好的,因为ClassOne会被加载两次 以下是我已有的代码: public class ClassOne { public ClassOne(ClassPojo classPojo) { // I want to change the text of this

我有一个Android应用程序,它使用Back命令返回开始屏幕

开始屏幕有一个标签,里面有一个数字,我想在使用back命令时更新这个标签

我可以用back命令中的代码找到一个解决方案,但我不知道我的方法是否是最好的,因为ClassOne会被加载两次

以下是我已有的代码:

public class ClassOne {

    public ClassOne(ClassPojo classPojo) {

        // I want to change the text of this label when calling the back command
        labelOne.setText(classPojo.getStringTest());
        formOne.show();
    }

}

public class ClassTwo {
        
    public ClassTwo(Form a , ClassPojo classPojo) {     
        Command back = new Command("A") {
            @Override
            public void actionPerformed(ActionEvent evt) {

                // I am adding the new value for the label here inside the back command

                classPojo.setStringTest("testing");
                a.showBack(); 
                new ClassOne(classPojo);
            }
        };
        formTwo.setBackCommand(back);
    }

重新创建表单实例的开销可以忽略不计,因此这不会是一个问题,但近年来,我们尝试更多地重用表单实例。不是因为表演


好处在于较小的行为,例如表单中的滚动位置。这些都很难复制。

我不确定问题出在哪里,你的例子有点泛化。但是,不重新创建
startScreen
表单实例的完整示例如下:

        Form startScreen = new Form("Start screen", BoxLayout.y());
        Wrapper<Integer> count = new Wrapper<>(1);
        Label numberLabel = new Label(count.get() + "");
        Button button1 = new Button("Go to Form 2");
        startScreen.addAll(numberLabel, button1);
        startScreen.show();

        button1.addActionListener(l -> {
            Form form2 = new Form("Form 2", BoxLayout.y());
            Label label = new Label("Use the back button");
            form2.add(label);
            form2.getToolbar().setBackCommand("Back", Toolbar.BackCommandPolicy.ALWAYS, ll -> {
                count.set(count.get() + 1);
                numberLabel.setText(count.get() + "");
                startScreen.showBack();
            });
            form2.show();
        });

在我看来,是否重新创建表单实例应该根据具体情况进行评估。根据我的温和观点,在考虑的变量中,还有代码的可读性和它的功能,特别是在复杂的情况下。

在测试过程中,我发现了一个简单的解决方案,即向构造函数添加标签。我希望这个片段能有所帮助

public ClassTwo(Form a, ClassPojo classPojo, Label label) { 

    Command back = new Command("A") {
        @Override
        public void actionPerformed(ActionEvent evt) {

        label.setText(classPojo.getStringTest());
        a.showBack(); 
     }
};

嗨,Shai,谢谢你的快速回复。我无法完全理解您的解释:您的意思是执行类似form.revalidate()的操作而不是调用该类吗?form revalidate不会重新加载数据,因此这不是一个真正的选项。您需要实现某种形式的对象绑定到表单。一般来说,当你回去时,你希望得到“相同的表格”/非常感谢你发布这个答案。这个解决方案非常有效。我只需要几天来测试一下。在测试过程中,我发现了一个简单的解决方案,它一直就在我眼前,出于某种原因,我没有看到它。将标签添加到构造函数(下面的代码)也可以在运行back命令时更新文本。否则,我将不得不对现有代码进行太多更改。但在我未来的一个项目中,你的代码肯定会派上用场。它简单、优雅、高效。我以前没有使用过包装器类。这也是一个有趣的方法。还有:Toolbar.BackCommandPolicy.ALWAYS做什么?谢谢你的评论<代码>工具栏。BackCommandPolicy有六个选项,如下所述:。具体来说,“始终”选项始终在工具栏中显示“后退”命令。对于iOS构建来说,这是必不可少的,但例如在Android上,您可能只对使用硬件后退按钮感兴趣,而不在工具栏中插入文本或图标:因此,有六种不同的选项可用。模拟器的一些Android皮肤允许您模拟按下硬件后退按钮。
public ClassTwo(Form a, ClassPojo classPojo, Label label) { 

    Command back = new Command("A") {
        @Override
        public void actionPerformed(ActionEvent evt) {

        label.setText(classPojo.getStringTest());
        a.showBack(); 
     }
};