Android 获取 TableLayout 中的所有 TableRow
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3327599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get all TableRow's in a TableLayout
提问by eugene
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.
我一直在寻找关于如何在 TableLayout 中获取所有 TableRow 的时间。我已经知道如何动态添加和删除行,但我需要遍历所有行并有选择地删除其中一些行。
I think I can come up with a work around, but I'm trying to avoid crude hacks in my app.
我想我可以想出一个解决办法,但我正在努力避免我的应用程序中的粗暴攻击。
回答by Quintin Robinson
Have you tried using getChildCount()
and getChildAt(int)
respectively?
你试过分别使用getChildCount()
和getChildAt(int)
吗?
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);
}
}
}
回答by user2963492
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);
}
}
}
回答by Jason Cidras
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.
}
}
回答by Spektakulatius
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
如果您尝试删除 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);
}
}
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
我认为您的主要问题是,如果您删除所有行都将被新放置的行,那么如果删除 ID = 3 的行,则 ID = 5 的行现在是 ID = 4
so maybe you have to reset the counter and iterate again or you generate the table new after you cleared all rows
所以也许您必须重置计数器并再次迭代,或者在清除所有行后生成新表
回答by SuSh
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);
}
}