RemoveAllViews does not work

3

How about, I have an android application in which I need to use a tablelayout to search for products, everything works well when searching for the first product, but when I search for a second product the rows and labels of the first one are not removed, I have already placed the removeAllViews but it does not work, maybe I'm putting it wrong or I do not know what may be happening, I leave the code snippet and hopefully someone can guide me:

protected void onPostExecute(Void result) {
        super.onPostExecute(result);


       // listaProd.setAdapter(result);
        tl  = (TableLayout) findViewById(R.id.miTableLayout);
        tl.removeAllViews();

        TableRow.LayoutParams layoutFila=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
        TableRow.LayoutParams layoutFecha=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
        TableRow.LayoutParams layoutMuestra=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
        TableRow.LayoutParams layoutResultado=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);


        filas =new TableRow(getApplicationContext());
        filas.setLayoutParams(layoutFila);


        cabeceraCantidad = new TextView(getApplicationContext());
        cabeceraTienda = new TextView(getApplicationContext());
        cabeceraEstilo = new TextView(getApplicationContext());

        cabeceraTienda.setText("Tienda");
        cabeceraTienda.setBackgroundResource(R.drawable.tabla_celda_cabecera);
        cabeceraTienda.setTextColor(Color.WHITE);

        cabeceraCantidad.setText("Total");
        cabeceraCantidad.setTextColor(Color.WHITE);
        cabeceraCantidad.setBackgroundResource(R.drawable.tabla_celda_cabecera);

        cabeceraEstilo.setText("Nombre del Producto");
        cabeceraEstilo.setTextColor(Color.WHITE);
        cabeceraEstilo.setBackgroundResource(R.drawable.tabla_celda_cabecera);

        filas.addView(cabeceraTienda);
        filas.addView(cabeceraCantidad);
        filas.addView(cabeceraEstilo);

        //tvTiendas = new TextView(getApplicationContext());
        tl.addView(filas);
            for(int m = 0; m < listaCabecera.size()-1; m++)
            {
                cabeceraTallas = new TextView(getApplicationContext());

                for(int n = m+1; n < listaCabecera.size(); n++)
                {
                    if (cabeceraTallas.getParent() != null)
                    {
                        ((ViewGroup) cabeceraTallas.getParent()).removeView(cabeceraTallas);
                    }


                    if(listaCabecera.get(m).getTalla().equals(listaCabecera.get(n).getTalla()))
                    {
                        m++;
                    }
                    else {
                        cabeceraTallas.setText(listaCabecera.get(m).getTalla());
                        cabeceraTallas.setTextColor(Color.WHITE);
                        cabeceraTallas.setBackgroundResource(R.drawable.tabla_celda_cabecera);
                        filas.addView(cabeceraTallas);
                    }

                }
            }

        for(int k =0 ; k < listaCabecera.size()-1; k++)
            {

                filasdatos =new TableRow(getApplicationContext());
                filasdatos.setLayoutParams(layoutFila);

                filasCantidad = new TableRow(getApplicationContext());
                filasCantidad.setLayoutParams(layoutFila);
                //filasdatos.removeAllViews();

                tvTiendas = new TextView(getApplicationContext());
                tvTotal  = new TextView(getApplicationContext());
                totali = new TextView(getApplicationContext());
                tvEstilo = new TextView(getApplicationContext());

                for(int l = k+1; l < listaCabecera.size(); l++)
                {
                    if (tvTiendas.getParent() != null
                            || tvTotal.getParent() != null
                            || tvEstilo.getParent() != null)
                    {
                        ((ViewGroup) tvTiendas.getParent()).removeView(tvTiendas);
                        ((ViewGroup) tvTotal.getParent()).removeView(tvTotal);
                        ((ViewGroup) tvEstilo.getParent()).removeView(tvEstilo);
                    }

                    if(listaCabecera.get(k).getTienda().equals(listaCabecera.get(l).getTienda())) {
                        k++;
                    }
                    else
                    {
                        tvTiendas.setText(listaCabecera.get(k).getTienda());
                        tvTotal.setText(listaCabecera.get(k).getCantidad());
                        tvEstilo.setText(listaCabecera.get(k).getNombre_Producto());

                        sumaTotal =Integer.parseInt(tvTotal.getText().toString());
                        total += sumaTotal;

                        tvTotal.setText(String.valueOf(total));

                        tvTiendas.setTextColor(Color.BLACK);
                        tvTiendas.setBackgroundResource(R.drawable.tabla_celda);

                        tvTotal.setTextColor(Color.BLACK);
                        tvTotal.setBackgroundResource(R.drawable.tabla_celda);

                        tvEstilo.setTextColor(Color.BLACK);
                        tvEstilo.setBackgroundResource(R.drawable.tabla_celda);

                        filasdatos.addView(tvTiendas);

                        filasdatos.addView(tvTotal);
                        filasdatos.addView(tvEstilo);
                    }

                }



                tl.addView(filasdatos);



                for(int z = 0; z < listaCabecera.size(); z++)
                {
                    tvCantidad = new TextView(getApplicationContext());


                        if (tvCantidad.getParent() != null)
                        {
                            ((ViewGroup) tvCantidad.getParent()).removeView(tvCantidad);

                        }

                        if(tvTiendas.getText().equals(listaCabecera.get(z).getTienda())) {
                            tvCantidad.setText(listaCabecera.get(z).getCantidad());

                            tvCantidad.setTextColor(Color.BLACK);
                            tvCantidad.setBackgroundResource(R.drawable.tabla_celda);
                            filasdatos.addView(tvCantidad);
                        }
                }

            }
        }
    
asked by Sandra 14.09.2016 в 16:25
source

1 answer

1

reviewing your code, you are taking the reference of TableLayout again and want to delete the views it contains:

    tl  = (TableLayout) findViewById(R.id.miTableLayout);
    tl.removeAllViews();

Do not get the reference again and try to eliminate the views, take the current instance:

    //tl  = (TableLayout) findViewById(R.id.miTableLayout);
    tl.removeAllViews();
    
answered by 14.09.2016 в 20:19