【问题标题】:Get all TableRow's in a TableLayout获取 TableLayout 中的所有 TableRow
【发布时间】:2011-03-20 15:32:43
【问题描述】:

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

我想我可以想出一个变通办法,但我试图避免在我的应用中出现粗暴的黑客攻击。

【问题讨论】:

  • -1 for:你应该接受一个答案!让人们编写响应和代码然后甚至不查看响应是不公平的。
  • 人们很容易因一场简单的车祸或心脏病发作而去世。不要太担心答案接受等。

标签: android tablelayout tablerow


【解决方案1】:

您是否尝试过分别使用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 当然,只是要注意,我确实将这些方法引用链接到它们的实际 sdk 文档。此外,添加到我的答案中的示例似乎是在扩展 TableLayout 的上下文中,而不是使用视图的引用,仅供参考。最后一点,如果实际上在循环中从集合中删除项目(索引重新计算等),请注意。
  • 顺便说一句,getChildAt(int) 返回一个 'View' 而不是 'TableRow'!
  • 同样重要的是要注意,在我们随意强制转换为 TableRow 之前,我们真的应该使用 instanceof 进行类型完整性检查,否则我们最终可能会出现 InvalidCastException。
  • TableLayout继承了lot of methods,特别是getChildCountgetChildAt继承自android.view.ViewGroup
【解决方案2】:
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);
    }
}

【讨论】:

【解决方案3】:

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

    //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】:

    如果你有其他类型的视图

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

    【讨论】:

      【解决方案5】:

      如果您尝试删除 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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-17
        • 2014-10-01
        • 2012-07-06
        • 1970-01-01
        • 1970-01-01
        • 2022-12-14
        • 2015-02-28
        相关资源
        最近更新 更多