2013-03-18 63 views
0

在我的應用程序中,我有一個有點複雜佈局的表格:它是一個3x3表格,頂部和左側是靜態標題,其他四個單元格每個都包含一個表格與我的信息。以編程方式替換TableLayout的單元格(使用複雜的佈局)

此內部表有兩個選項,我以編程方式選擇要使用哪兩個,並將它們添加到表中。到現在爲止還挺好。

現在,當更新表格中的信息時,第二個表格被添加到單元格中,而不是被替換的原始表格。所以在幾次更新之後,我的主表變得很大,並且其中的單元格很高,並且有很多內部表格。當然,這不是主意。

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

如何才能做到這一點?

+0

有趣的是看到我的問題被編輯(這個詞「不成功」不是我的),我想錯了。我確實找回了一個id(簡單的檢查:它是一個初始化爲= 0的int,後來它是!= 0),並且有理由相信它是正確的id。它還能是什麼? – Wouter 2013-03-18 15:49:45

回答

0

最後通過稍微不同的路線解決了它。

我錯誤地認爲,當您再次將視圖添加到單元格時,它是附加到它的第二個視圖。相反,我的表正在追加更多行到它自己。

而且還這麼多關於TableLayout.removeView()

該解決方案的實際運作沒有文檔:在setTable一條線,可以消除該表中的所有觀點開始。這清理了這張桌子,之後我可以重新佈置我的佈局,而且全部都是應該的。

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