29

TableLayout ですべての TableRow を取得する方法を何時間も探していました。行を動的に追加および削除する方法は既に知っていますが、すべての行をループして、それらの一部を選択的に削除する必要があります。

回避策を思い付くことができると思いますが、アプリでの雑なハッキングを回避しようとしています。

4

5 に答える 5

52

それぞれと を使っ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);
        }
    }
}
于 2010-07-25T01:18:34.557 に答える
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);
        }
    }
}
于 2013-11-13T06:04:57.000 に答える
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.
        }
    }
于 2013-06-25T13:31:16.607 に答える
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);
    }
}
于 2013-01-03T14:08:39.960 に答える