Search code examples
javacodenameone

i just need to add details to the list, one at a time using codename one


am working on a project where I need to add a list of children(including their details) in a database table... but before the user sends the details I need to see that list, the maximum is 7 children. I have created a list and an add action but I cant add the child details to the list. using codename one I have,

    public void childscreen()
        {
            /*-------DETAILS TAKEN HERE------*/
            Form regscreen = new Form("Fill Childs details");
        TableLayout tl;
        
         int spanButton = 2;
        if(Display.getInstance().isTablet()) {
            tl = new TableLayout(7, 2);
        } else {
            tl = new TableLayout(14, 1);
            spanButton = 1;
        }
        tl.setGrowHorizontally(true);
        regscreen.setLayout(tl);
        
        regscreen.addComponent(new Label("First Name"));
        TextField txtname = new TextField();
        regscreen.addComponent(txtname);
        
         regscreen.addComponent(new Label("Middle Name"));
        TextField txtname2 = new TextField();
        regscreen.addComponent(txtname2);
        
         regscreen.addComponent(new Label("Surname"));
        TextField txtname3 = new TextField();
        regscreen.addComponent(txtname3);
        
        regscreen.addComponent(new Label("Birth Certificate/Notification No"));
        TextField txtbirth = new TextField();
    //    txtidno.setConstraint(TextArea.NUMERIC);
        regscreen.addComponent(txtbirth);
        
        regscreen.addComponent(new Label("Date of Birth"));
        PickerComponent date = PickerComponent.createDate(new Date());
        regscreen.addComponent(date);   
        
        Button btnadd = new Button("Add");
        TableLayout.Constraint cn = tl.createConstraint();
        cn.setHorizontalSpan(spanButton);
        cn.setHorizontalAlign(Component.RIGHT);
        regscreen.addComponent(cn, btnadd);
        
        Validator v = new Validator();
        v.addConstraint(txtname, new LengthConstraint(3)).
                addConstraint(txtname2, new LengthConstraint(3,"Enter Middle Name")).
                addConstraint(txtname3, new LengthConstraint(3)).
                addConstraint(txtbirth, new LengthConstraint(10)).
                addConstraint(txtbirth, new LengthConstraint(4));
    //            addConstraint(txtname2, new RegexConstraint("txtname2", "Must be valid phone number"));
                
        v.addSubmitButtons(btnadd);
            /*-------DETAILS TAKEN HERE------*/
            
            Form watoto=new Form("CHILDREN DETAILS", new BorderLayout());
            Container list=new Container(BoxLayout.y());
            list.setScrollableY(true);
            for(int inter=1; inter<=7; inter++)
            {
                MultiButton mb=new MultiButton("Child Details "+inter);
                mb.setTextLine2("Add");
                mb.addActionListener(new ActionListener() 
                {
                    @Override
                    public void actionPerformed(ActionEvent evt) 
                    {
                        regscreen.show();
                    }
                });
                list.add(mb);
            }
            watoto.add(CENTER, list);
            watoto.show();
        }

any help thanks in advance


Solution

  • You have two separate forms. Both work. But it seems you're recreating the child list form every time so you're getting back to it and it's empty because it's a new form.

    The child list form needs to be in a wider scope than the add child form. A simple way to do it is to pass watoto to the add child form and when a child is added invoke a method to show back.

    But this alone won't solve your problem. You need to separate the data from the view. Right now you don't have a "concept" of children in your system. You only have buttons and UI elements. These should be separated.