以编程方式替换TableLayout的单元格(具有复杂的布局)

时间:2013-03-18 15:44:49

标签: android android-tablelayout

在我的应用程序中,我有一个布局有些复杂的表:它是一个3x3表,顶部和左侧是静态标题,另外四个单元格再次包含一个表格,包含我的信息。

这个内部表有两个选项,我以编程方式选择要使用的两个选项中的哪一个,并将它们添加到表中。到目前为止一切都很好。

现在,在更新表格中的信息时,会向单元格添加第二个表格,而不是替换原始格式的表格。因此,经过一些更新后,我的主表变得庞大,并且其中的单元格非常高,有许多内部表。当然这不是主意。

private int setTable(int minAPI, int maxAPI, TableLayout tableId) {

    // Get the levels that belong to the API.
    String minLevel = getLevel(minAPI);
    String maxLevel = getLevel(maxAPI);

    // Get an inflater to inflate the table layouts.
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // Get the layout of the API/Level table. 
    // Which one to use for the levels depends on whether the levels are 
    // the same or not.
    TextView textLevelFrom = null;
    TextView textLevelTo = null;
    TableLayout tableLayout = null;

    if (minLevel.equals(maxLevel)) {
        Log.v(TAG, "Setting up api_table1.");
        tableLayout = (TableLayout) inflater.inflate(R.layout.api_table1, tableId);
        textLevelFrom = (TextView) tableLayout.findViewById(R.id.textLevel);
        textLevelFrom.setText(minLevel);        
        textLevelFrom.setTextColor(getColour(minAPI));  
    }

    else {
        Log.v(TAG, "Setting up api_table2.");
        tableLayout = (TableLayout) inflater.inflate(R.layout.api_table2, tableId);
        textLevelFrom = (TextView) tableLayout.findViewById(R.id.textLevelFrom);
        textLevelFrom.setText(minLevel);        
        textLevelFrom.setTextColor(getColour(minAPI));  
        textLevelTo = (TextView) tableLayout.findViewById(R.id.textLevelTo);
        textLevelTo.setText(maxLevel);
        textLevelTo.setTextColor(getColour(maxAPI));
    }

    Log.v(TAG, "Populating the table.");

    TextView textAPIFrom = (TextView) tableLayout.findViewById(R.id.textAPIFrom);
    textAPIFrom.setText(""+minAPI);     
    textAPIFrom.setTextColor(getColour(minAPI));

    TextView textAPITo = (TextView) tableLayout.findViewById(R.id.textAPITo);
    textAPITo.setText(""+maxAPI);
    textAPITo.setTextColor(getColour(maxAPI));

    textLevelFrom.setText(minLevel);        
    textLevelFrom.setTextColor(getColour(minAPI));

    return tableLayout.getId();
}

TableLayout TableId以前是通过

之类的调用找到的
tableCurrentGeneral = (TableLayout) findViewById(R.id.CurrentGeneral);

四个细胞中的每个细胞一个。主表在main.xml中定义;这些表的单元格是一个空的TableLayout。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.squirrel.hkairpollution.MySupportMapFragment"
    />
<!-- class="com.google.android.gms.maps.SupportMapFragment" -->

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tableAPI" 
        android:background="@color/translucent_white"
        android:layout_alignBottom="@id/map">

        <!-- First row: headers. -->
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="@string/general" 
                android:textStyle="bold" 
                android:textColor="@color/black"
                android:gravity="center" 
                android:paddingRight="8dip" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/roadside" 
                android:textStyle="bold" 
                android:textColor="@color/black"
                android:gravity="center" />

        </TableRow>

        <!-- Second row: current API -->                
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/current" 
                android:textStyle="bold" 
                android:textColor="@color/black"
                android:paddingRight="5dip"
                android:gravity="center" />

            <!-- Current API, general stations -->
            <TableLayout
                android:id="@+id/CurrentGeneral"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:paddingRight="8dip" />

            <TableLayout
                android:id="@+id/CurrentRoadside"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </TableRow>

        <!-- Third row: forecast API -->
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/forecast" 
                android:textStyle="bold" 
                android:textColor="@color/black"
                android:paddingRight="5dip"
                android:gravity="center" />

            <!-- Forecast API, general stations -->
            <TableLayout
                android:id="@+id/ForecastGeneral"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:paddingRight="8dip" />

            <TableLayout
                android:id="@+id/ForecastRoadside"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout>

    <ProgressBar
        android:id="@+id/progressBar1"
        android:visibility="invisible"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:layout_alignTop="@id/map" 
        android:layout_alignLeft="@id/map"/>

    <ImageView
        android:id="@+id/refresh"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:contentDescription="@string/refresh"
        android:layout_alignTop="@id/map" 
        android:layout_alignLeft="@id/map"
        android:src="@drawable/ic_menu_refresh" />

</RelativeLayout>

该行

tableLayout = (TableLayout) inflater.inflate(R.layout.api_table1, tableId);

正确链接api_table1 resp。 api_table2布局到所需的单元格,但是在添加此布局的所有时间,它都没有被替换。

我试图回复刚刚添加的视图的ID,但稍后将其删除:

tableAPI.removeView(findViewById(tableCurrentRoadsideId));

如何做到这一点?

1 个答案:

答案 0 :(得分:0)

最后通过稍微不同的路线解决了它。

我错误地认为,当您再次将视图附加到单元格时,它是附加到其上的第二个视图。相反,我的表正在为自己添加更多行。

还有很多关于TableLayout.removeView()

实际运作的文档

解决方案:在setTable开头,删除该表中的所有视图。这清除了这张桌子,之后我可以重新贴上我的布局,一切都应该如此。

private void setTable(int minAPI, int maxAPI, TableLayout tableId) {
    tableId.removeAllViews();
    ...