style gridlayout example dynamically columns column atributos 3x3 android tablelayout tablerow

android - gridlayout - Obtener todos TableRow en un TableLayout



tablelayout android example dynamically (5)

He estado buscando horas sobre cómo obtener todos los TableRow 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.

Creo que puedo encontrar una solución alternativa, pero estoy tratando de evitar trucos crudos en mi aplicación.


He estado buscando lo mismo por un tiempo. Para mí, estaba buscando cómo verificar vistas en el diseño de mi tabla. 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. } }


Si intenta eliminar TableRows, el contador de ID disminuirá, por lo que los pls usan este bucle para eliminar ... de lo contrario, si no desea eliminar, puede usar 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 principal problema es si elimina las filas que todas las filas se colocarán nuevamente, de modo que la fila con el ID = 5 ahora sea ID = 4 si elimina la fila con el ID = 3

así que quizás tenga que restablecer el contador e iterar de nuevo o generar la tabla nueva después de borrar todas las filas


si tienes 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); } } }


¿Ha intentado usar 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); } } }


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); } }