0

私のアプリには、やや複雑なレイアウトのテーブルがあります。これは3x3のテーブルで、上部と左側が静的ヘッダーであり、他の4つのセルにはそれぞれ私の情報を含むテーブルが含まれています。

この内部テーブルには2つのオプションがあり、プログラムで2つのうちどちらを使用するかを選択し、それらをテーブルに追加します。ここまでは順調ですね。

これで、テーブルの情報を更新するときに、元のテーブルが置き換えられる代わりに、2番目のテーブルがセルに追加されます。したがって、いくつかの更新の後、私のメインテーブルは巨大になり、その中のセルは非常に高くなり、多くの内部テーブルがあります。もちろん、それは考えではありません。

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

4つのセルごとに1つ。メインテーブルは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応答を正しくリンクします。api_table2は目的のセルにレイアウトされますが、このレイアウトが追加されるたびに、置き換えられることはありません。

追加したばかりのビューのIDを返して、後で削除しようとして失敗しました。

tableAPI.removeView(findViewById(tableCurrentRoadsideId));

これはどのように行うことができますか?

4

1 に答える 1

0

最後に、わずかに異なるルートでそれを解決しました。

ビューをセルに再度アタッチすると、それにアタッチされるのは2番目のビューであると誤って信じていました。代わりに、私のテーブルはそれ自体にさらに行を追加していました。

また、実際の動作に関するドキュメントがないため、これだけです。TableLayout.removeView()

解決策:setTableそのテーブルからすべてのビューを削除する行の先頭。これでこのテーブルがクリアされ、その後、レイアウトを再アタッチできます。すべてが正常です。

private void setTable(int minAPI, int maxAPI, TableLayout tableId) {
    tableId.removeAllViews();
    ...
于 2013-03-18T16:37:55.677 に答える