获取 TableLayout 中的所有 TableRow

发布于 2024-09-11 19:03:24 字数 133 浏览 6 评论 0原文

我花了几个小时寻找如何在 TableLayout 中获取所有 TableRow。我已经知道如何动态添加和删除行,但我需要循环所有行并有选择地删除其中一些行。

我想我可以想出一个解决方法,但我正在努力避免我的应用程序中出现粗暴的黑客行为。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

数理化全能战士 2024-09-18 19:03:25

您是否尝试过使用 getChildCount()< /a> 和 getChildAt(int)分别是?

循环应该相当容易:

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

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);
        }
    }
}
失去的东西太少 2024-09-18 19:03:25

如果您有其他类型的视图

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

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);
        }
    }
}
谎言 2024-09-18 19:03:25

一段时间以来我一直在寻找同样的东西。对我来说,我正在寻找如何检查表格布局中的视图。我这样做是通过:

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

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.
        }
    }
放飞的风筝 2024-09-18 19:03:25

如果您尝试删除 TableRows,ID 计数器将会减少,因此请使用此循环来删除...否则,如果您不想删除,您可以使用上面的一些循环: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);
        }        
    }

这会清除所有行!

我认为你的主要问题是,如果你删除所有行都将被新放置的行,那么如果你删除 ID = 3 的行,则 ID = 5 的行现在是 ID = 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

万水千山粽是情ミ 2024-09-18 19:03:25
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);
    }
}
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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文