Android - No sirve removeAllViews()

 
Vista:
Imágen de perfil de Sandra

No sirve removeAllViews()

Publicado por Sandra (4 intervenciones) el 19/09/2016 19:31:38
Que tal, estoy realizando un proyecto en android el cual consiste en mostrar datos de un webservice en una tabla, uso tablelayout para mostrarlo, hago la búsqueda de un producto y me muestra los datos, pero cuando quiero consultar una segunda vez, no me borra las filas del anterior producto, utilizo removeAllViews pero no funciona, y no entiendo por qué, les dejo parte de mi código y espero que alguien me ayude a visualizar el error que estoy cometiendo, gracias.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
            {
                //tl.removeAllViews();
                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);
                }
        }
    }
    //tl.removeAllViews();
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

No sirve removeAllViews()

Publicado por Bladimir (8 intervenciones) el 21/09/2016 13:16:58
Hola Sandra. Yo tuve la necesidad de construir una tabla dinámica para visualizar unos datos. Seguí este link para construir la tabla https://programaressencillo.wordpress.com/2014/11/22/android-tablas-dinamicas-en-android/ . Obviamente hice las modificaciones necesarias. El asunto es que en la clase Tabla cree un método llamado eliminarfilas el cual tiene el siguiente código:
1
2
3
4
public void eliminarFilas()
{
    tabla.removeAllViews();
}
En la clase que ejecuta la construcción de la tabla dinámica, antes de proceder a construirla, llamo a ese método para borrar la tabla anterior. El codigo que ejecuto es este:
1
2
3
4
5
protected void eliminarfilas() {
	// TODO Auto-generated method stub
	Tabla tabla = new Tabla(this, (TableLayout)findViewById(R.id.tabla));
	tabla.eliminarFilas();;
}
De ese modo yo resuelvo el problema. Espero haberte ayudado en algo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Sandra

No sirve removeAllViews()

Publicado por Sandra (4 intervenciones) el 21/09/2016 15:47:07
Hola, muchas gracias por responder y de hecho me basé en ese artículo y me funcionaba, pero de la nada dejó de funcionar :(, ya he borrado todo pero nada.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar