2010-07-25 27 views
25

He estado buscando durante horas sobre cómo conseguir todos los TableRow de en un TableLayout. Ya sé cómo agregar y eliminar filas dinámicamente, pero necesito recorrer todas las filas y eliminar selectivamente algunas de ellas.Obtener todos TableRow de TableLayout en un

creo que puedo llegar a una solución alternativa, pero estoy tratando de evitar los cortes de crudo en mi aplicación.

+7

-1 para: usted debe aceptar una respuesta! Es injusto que las personas escriban respuestas y códigos y luego ni siquiera miren las respuestas. – John

+3

Las personas pueden pasar fácilmente por tener un simple accidente automovilístico o un ataque al corazón. No te preocupes demasiado por la aceptación de la respuesta y tal. – Ehsan

Respuesta

42

¿Ha intentado utilizar getChildCount() y getChildAt(int) respectivamente?

debería ser bastante fácil en un bucle:

for(int i = 0, j = table.getChildCount(); i < j; i++) { 
    View view = table.getChildAt(i); 
    if (view instanceof TableRow) { 
     // then, you can remove the the row you want... 
     // for instance... 
     TableRow row = (TableRow) view; 
     if(something you want to check) { 
      table.removeViewAt(i); 
      // or... 
      table.removeView(row); 
     } 
    } 
} 
+0

No he visto nada en la documentación de Android SDK sobre estos métodos (tal vez simplemente lo pasé por alto). Lo intentaré y te lo haré saber. Gracias por su ayuda. – eugene

+0

@eugene Absolutamente, solo para observar, vinculé esas referencias de método a la documentación real de SDK para ellos. También parece que el ejemplo que se agregó a mi respuesta está en el contexto de extender el 'TableLayout' en lugar de usar una referencia de la vista, solo FYI. Una nota final, hay advertencias si realmente elimina los elementos de una colección en un bucle (recálculos de índices y similares) así que ten cuidado. –

+0

BTW, getChildAt (int) devuelve una 'Vista' y no 'TableRow'! – PCoder

0
for(int i = 0, j < table.getChildCount(); i < j; i++){ 
    // then, you can remove the the row you want... 
    // for instance... 
    TableRow row = (TableRow) table.getChildAt(i); 
    if(something you want to check) { 
     removeViewAt(i); 
     // or... 
     removeView(row); 
    } 
} 
+1

Esta es más o menos la misma respuesta que [este] (http://stackoverflow.com/a/3327612). –

2

He estado buscando lo mismo por un tiempo. Para mí, estaba buscando cómo ver las vistas en el diseño de mi mesa. Hice esto por:

//This will iterate through your table layout and get the total amount of cells. 
    for(int i = 0; i < table.getChildCount(); i++) 
    { 
     //Remember that .getChildAt() method returns a View, so you would have to cast a specific control. 
     TableRow row = (TableRow) table.getChildAt(i); 
     //This will iterate through the table row. 
     for(int j = 0; j < row.getChildCount(); j++) 
     { 
      Button btn = (Button) row.getChildAt(j); 
      //Do what you need to do. 
     } 
    } 
14

si tiene otro tipo de vista

TableLayout layout = (TableLayout) findViewById(R.id.IdTable); 

for (int i = 0; i < layout.getChildCount(); i++) { 
    View child = layout.getChildAt(i); 

    if (child instanceof TableRow) { 
     TableRow row = (TableRow) child; 

     for (int x = 0; x < row.getChildCount(); x++) { 
      View view = row.getChildAt(x); 
      view.setEnabled(false); 
     } 
    } 
} 
0

Si intenta quitar el contador TableRows ID disminuirá así que por favor utilice este bucle para eliminar ... else si no desea eliminar puede utilizar algunos de los bucles anteriores: D

TableLayout table = (TableLayout) findViewById(R.id.tblScores); 
    for(int i = 0; i < table.getChildCount(); i = 0) 
    { 
     View child = table.getChildAt(0); 
     if (child != null && child instanceof TableRow) 
     { 
      TableRow row = (TableRow) child; 
      row.removeAllViews(); 
      table.removeViewAt(i); 
     }   
    } 

¡Esto borra todas las filas!

Creo que su problema principal es si se quita filas que todas las filas serán recién colocado de manera que la fila con el ID = 5 es ahora ID = 4 si se elimina la fila con ID = 3

así que tal vez usted tiene que restablecer el contador y repetir de nuevo o se genera la tabla de nuevo después de que borró todas las filas

Cuestiones relacionadas