Подтвердить что ты не робот

Получить весь TableRow в TableLayout

Я искал часы о том, как получить все TableRow в TableLayout. Я уже знаю, как добавлять и удалять строки динамически, но мне нужно перебрать все строки и выборочно удалить некоторые из них.

Я думаю, что могу придумать работу, но я стараюсь избегать грубых хаков в своем приложении.

4b9b3361

Ответ 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);
        }
    }
}

Ответ 2

если у вас есть вид другого типа

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

Ответ 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

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

Ответ 5

Если вы попытаетесь удалить TableRows, счетчик ID уменьшится, поэтому PLS использует этот цикл для удаления... else, если вы не хотите удалять, вы можете использовать некоторые из циклов выше: 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);
        }        
    }

Это очищает все строки!

Я думаю, что ваша основная проблема заключается в том, что вы удаляете строки, в которые все строки будут размещены вновь, так что строка с идентификатором = 5 теперь будет ID = 4, если вы удалите строку с ID = 3

так что, возможно, вам нужно reset счетчик и снова повторить или вы сгенерируете таблицу new после того, как вы очистили все строки