How to Remove an Element from a Container Array

16 views
Skip to first unread message

rube...@yahoo.com

unread,
Apr 9, 2017, 11:37:52 AM4/9/17
to CodenameOne Discussions
Hi,

My intention is that when i make a long press on an element there should be displayed a warning message and then remove the element.

At first put the code of the long press on the component "lead" but does nothing.

The problem is that to trigger the long press I had to place the code on the creation of the new container (at Steve's suggestion); When I place the remove method it tells me that the container has to be initialized.

My Code:

The "creaContenedorCompra2" method returns the container array.

    private Container creaContenedorCompra2(Form fmCompra2, Compra1 cR1) {

        Container cnCompras = new Container();
        cnCompras.setScrollableY(true);

        Vector vCompra2 = dT.getCompra2(cR1);
        for (int i = 0; i < vCompra2.size(); i++) {
            Compra2 cR2 = (Compra2) vCompra2.elementAt(i);
            cnCompras.add(creaContenedorItemCompra2(fmCompra2, cR1, cR2));
        }
        cnCompras.revalidate();

        return cnCompras;
    }

The "creaContenedorItemCompra2" method returns an element that will be part of the container array

    private Container creaContenedorItemCompra2(Form fmCompra2, Compra1 cR1, Compra2 cR2) {

        Container cnCompra = new Container() {
            @Override
            public void longPointerPress(int x, int y) {
                super.longPointerPress(x, y);

                Dialog dlg = new Dialog();
                dlg.setUIID("Label");
                boolean continuar = dlg.show(idioma.getMensaje19(), idioma.getMensaje13(), idioma.getSi(), idioma.getNo());
                if (continuar) {

//                 cnCompra.remove();                          Show message "Initialize variable cnCompra"

                    cR2.setCondicion(Constantes.REGISTRO_ELIMINADO);
                    cR2.setSincronizado(false);
                    Vector vCompra2 = dT.getCompra2(cR1);
                    for (int i = 0; i < vCompra2.size(); i++) {
                        Compra2 cR = (Compra2) vCompra2.elementAt(i);
                        if (cR.getCompra().equals(cR2.getCompra()) && cR.getArticulo().equals(cR2.getArticulo())) {
                            vCompra2.setElementAt(cR2, i);
                        }
                    }
                }
            }
        };
        TextArea articulo = new TextArea(2, 70);
        Label lbCantidad = new Label();
        Label lbPrecio = new Label();
        Label lbMonto = new Label();
        Label lbUM = new Label();
        TextArea cantidad = new TextArea();
        TextArea precio = new TextArea();
        TextArea monto = new TextArea();
        TextArea uM = new TextArea(1, 7);

        articulo.setUIID("MultiLine2");
        articulo.setFocusable(true);
        articulo.setEditable(false);
        articulo.setAlignment(TextArea.LEFT);
        articulo.setPropertyValue("serial", cR2.getSerial());

        lbCantidad.setText(idioma.getCantidad() + ":");
        lbCantidad.setUIID("MultiLine3");
        cantidad.setUIID("MultiLine3");
        cantidad.setFocusable(true);
        cantidad.setEnabled(true);

        lbPrecio.setText(idioma.getPrecio() + ":");
        lbPrecio.setUIID("MultiLine3");
        precio.setUIID("MultiLine3");
        precio.setFocusable(true);
        precio.setEnabled(true);

        lbMonto.setText(idioma.getMonto() + ":");
        lbMonto.setUIID("MultiLine3");
        monto.setUIID("LabelTotalBig");
        monto.setText("00.00");
        monto.setFocusable(true);
        monto.setEnabled(true);
        monto.setAlignment(TextArea.RIGHT);

        lbUM.setText(idioma.getUM() + ":");
        lbUM.setUIID("MultiLine3");
        uM.setUIID("MultiLine3");
        uM.setFocusable(true);
        uM.setEnabled(true);

        Button lbImagen = new Button();
        lbImagen.setUIID("TransparentButton");
        lbImagen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                actualizaCompra2(fmCompra2, cnCompra, cR1, cR2, Constantes.CAMBIAR);
            }
        }
        );

        articulo.setText(cR2.getNombreArticulo());
        uM.setText(cR2.getUnidadMedida());

        Double cnt = cR2.getCantidad();
        Double pre = cR2.getPrecio();
        Double mnt = 0d;

        mnt = pre * cnt;
        cantidad.setText(Double.toString(cnt));
        precio.setText(Double.toString(pre));
        if (mnt == 0d) {
            monto.setText(" 0.00");
        } else {
            monto.setText(Double.toString(mnt));
        }

        if (pre > 0d) {
            articulo.setUIID("MultiLine2Resaltado");
        } else {
            articulo.setUIID("MultiLine2");
        }
        if (cR2.getCarpetaImagenCategoria().equals("")) {
            cR2.setCarpetaImagenCategoria("generico.png");
        }
        try {
            lbImagen.setIcon(Image.createImage("/" + cR2.getCarpetaImagenCategoria()));
        } catch (IOException ex) {
            try {
                lbImagen.setIcon(Image.createImage("/generico.png"));
            } catch (IOException ex1) {
                Dialog.show(idioma.getError(), ex1.getMessage(), idioma.getContinuar(), null);
            }
        }

        Container cnCantidad = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        cnCantidad.addComponent(lbCantidad);
        cnCantidad.addComponent(cantidad);

        Container cnPrecio = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        cnPrecio.addComponent(lbPrecio);
        cnPrecio.addComponent(precio);

        BorderLayout border = new BorderLayout();
        border.setCenterBehavior(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE);
        Container cnMonto = new Container();
        cnMonto.setLayout(new BorderLayout());
        cnMonto.setLayout(border);
        cnMonto.addComponent(BorderLayout.CENTER, monto);
        cnMonto.setUIID("LabelTotalBig");

        Container cnUM = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        cnUM.addComponent(lbUM);
        cnUM.addComponent(uM);

        Container cnInfo = new Container();
        cnInfo.setLayout(new GridLayout(1, 3));
        cnInfo.addComponent(cnUM);
        cnInfo.addComponent(cnCantidad);
        cnInfo.addComponent(cnPrecio);

        Container cnDatos = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        cnDatos.addComponent(articulo);
        cnDatos.addComponent(cnInfo);

        Container cnS1 = new Container(new BorderLayout());
        cnS1.setLayout(new BoxLayout(BoxLayout.X_AXIS));
        cnS1.getStyle().setBorder(Border.createBevelRaised());
        cnS1.addComponent(new Label());

        Container cnPreItem = new Container(new BoxLayout(BoxLayout.X_AXIS));
        cnPreItem.addComponent(lbImagen);
        cnPreItem.addComponent(cnDatos);

        Container cnItem = new Container();
        cnItem.setLayout(new BorderLayout());
        cnItem.addComponent(BorderLayout.CENTER, cnPreItem);
        cnItem.addComponent(BorderLayout.EAST, cnMonto);

        cnCompra.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        cnCompra.addComponent(cnItem);;
        cnCompra.addComponent(cnS1);
        cnCompra.setPropertyValue("busqueda", cR2.getNombreArticulo() + cR2.getSerial());
        cnCompra.setLeadComponent(lbImagen);

        return cnCompra;
    }
Reply all
Reply to author
Forward
0 new messages