Get all TableRow's in a TableLayout

孤人 提交于 2019-12-03 07:35:47

问题


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.


回答1:


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



回答2:


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



回答3:


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



回答4:


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




回答5:


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


来源:https://stackoverflow.com/questions/3327599/get-all-tablerows-in-a-tablelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!