在TableLayout中获取所有的TableRow

28

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

我认为我可以想出一个解决方法,但我试图避免在我的应用程序中使用粗糙的hack。


8
-1 表示:你应该接受一个答案!让别人写回复和代码然后连看都不看一眼是不公平的。 - John
7
人们可能会因为一场简单的车祸或心脏病突发而轻易离世。不用过于担心答案是否被接受等问题。 - Dr. Ehsan Ali
5个回答

51

你尝试使用 getChildCount()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);
        }
    }
}

我在Android SDK文档中没有看到关于这些方法的内容(也许是我疏忽了)。我会尝试一下并告诉你。感谢你的帮助。 - eugene
@eugene 当然,只是要注意的是,我已经将这些方法引用链接到了它们的实际sdk文档。此外,看起来添加到我的答案中的示例是在扩展TableLayout的上下文中而不是使用视图的引用,只是提醒一下。最后一个注意事项是,在循环中实际删除集合中的项目时存在一些警告(索引重新计算等),所以请注意。 - Quintin Robinson
1
顺便提一下,getChildAt(int) 返回的是 'View' 而不是 'TableRow'! - PCoder
同样需要注意的是,在我们随意将对象转换为TableRow之前,我们应该使用instanceof进行类型检查,否则可能会出现InvalidCastException。 - Evan Steinkerchner
TableLayout inherits a lot of methods, in particular getChildCount and getChildAt are inherited from android.view.ViewGroup - lasec0203

16

如果你有其他观点

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

2

我已经寻找同样的事情有一段时间了。对我来说,我正在寻找如何检查我的表格布局中的视图。我通过以下方式实现了这一点:

    //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
如果您尝试删除表格行,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

所以也许你需要重置计数器并再次迭代,或者在清除所有行后重新生成表格


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
这个答案与这个差不多。 - Artjom B.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接