28

I've been looking for hours on how to get all TableRow's in a TableLayout. I already know how to add and delete rows dynamically, but I need to loop over all the rows and selectively delete some of them.

I think I can come up with a work around, but I'm trying to avoid crude hacks in my app.

Cristian
  • 198,401
  • 62
  • 356
  • 264
eugene
  • 2,519
  • 3
  • 17
  • 5
  • 8
    -1 for: You should accept an answer! It's unfair to have people write responses and code and then not even looking at the responses. – John Apr 04 '14 at 18:06
  • 7
    People can easily pass away by having a simple car accident or heart attack. Don't worry much about the answer acceptance and such. – Dr. Ehsan Ali Sep 21 '15 at 02:50

5 Answers5

51

Have you tried using getChildCount() and getChildAt(int) respectively?

Should be fairly easy in a loop:

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);
        }
    }
}
Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
  • I haven't seen anything in the Android SDK documentation about these methods (maybe I just overlooked it). I'll try that and let you know. Thanks for your help. – eugene Jul 25 '10 at 01:28
  • @eugene Absolutely, just to note, I did link those method references to the actual sdk documentation for them. Also it looks like the example that was added to my answer is in context of extending the `TableLayout` rather then using a reference of the view, just FYI. One final note, there are caveats if actually removing items from a collection in a loop (index recalculations and the like) so beware. – Quintin Robinson Jul 25 '10 at 02:55
  • 1
    BTW, getChildAt(int) returns a 'View' and not 'TableRow'! – PCoder Sep 28 '12 at 21:24
  • Also important to note that we should really have a type sanity check with instanceof before we just willy-nilly cast to TableRow, or we could end up with an InvalidCastException. – Evan Steinkerchner Mar 29 '15 at 22:12
  • `TableLayout` inherits a [lot of methods](https://developer.android.com/reference/android/widget/TableLayout#inherited-methods), in particular `getChildCount` and `getChildAt` are inherited from `android.view.ViewGroup` – lasec0203 Dec 11 '20 at 04:26
16

if you have other type view

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);
        }
    }
}
Denim Datta
  • 3,740
  • 3
  • 27
  • 53
user2963492
  • 161
  • 1
  • 2
2

I have been looking for the same thing for a while. For me, I was looking to how to check views in my table layout. I did this by:

    //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.
        }
    }
1

If you try to remove TableRows the ID Counter will decrease so pls use this loop for removing ... else if you dont want to remove you can use some of the loops above :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);
        }        
    }

This clears all rows!

I think your main problem is if you remove rows that all rows will be newly placed so that the row with the ID = 5 is now ID = 4 if you delete the row with ID = 3

so maybe you have to reset the counter and iterate again or you generate the table new after you cleared all rows

Pwnstar
  • 2,333
  • 2
  • 29
  • 52
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);
    }
}
bensiu
  • 24,660
  • 56
  • 77
  • 117
SuSh
  • 17
  • 1