0

エントリの場合、リストとして表示されるデータを取得するために Web サービスを使用しています。XML ファイルに 2X2 テーブル レイアウト形式の 4 つの属性があります。次に、Java コードで TableLayout クラスを使用して、結果を各 TableRow に格納します。

これが私のxmlコードです: tabrow.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableRow2"
        style="@style/BodyRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

<TableRow android:id="@+id/tableRow2"
        style="@style/BodyRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

<TextView
    android:id="@+id/attrib_value2"
    style="@style/LeftHeaderText"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:text="TextView"
    android:textColor="@color/textColor" />



        <TextView
            android:id="@+id/attrib_value5"
            style="@style/HourText"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="TextView"
            android:textColor="@color/textColor" android:gravity="right">
        </TextView>
    </TableRow>

<TableRow android:id="@+id/tableRow3"
        style="@style/BodyRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    <TextView
            android:id="@+id/attrib_value3"
            style="@style/BodyText"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="TextView"
            android:textColor="@color/textColor" >
        </TextView>

        <TextView
            android:id="@+id/attrib_value4"
            style="@style/BodyText"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="TextView"
            android:textColor="@color/textColor" >
        </TextView>


</TableRow>

</TableLayout>

各エントリを追加するために tabrow.xml が呼び出される別の xml コードを次に示します。

<TableLayout
            android:id="@+id/resultDetail"
            android:layout_width="fill_parent"
            style="@style/BodyRow"
            android:layout_height="wrap_content" >
         </TableLayout>

以下はJavaコードです。

public void fillWorkEntries(ArrayList<WorkEntry> workEntries) {
    try {
        TableLayout table = (TableLayout) WorkEntryScreenActivity.this
                .findViewById(R.id.resultDetail);

        table.removeAllViews();
        for (WorkEntry workEntry : workEntries) {

            TableLayout nestedtable = (TableLayout) LayoutInflater.from(this).inflate(
                    R.layout.tabrow, null);

            TableRow bodyRow = (TableRow) LayoutInflater.from(this).inflate(
                    R.id.tableRow2, null);



            ((TextView) bodyRow.findViewById(R.id.attrib_value2))
                    .setText(workEntry.getWorkRequestName());


            ((TextView) bodyRow.findViewById(R.id.attrib_value5))
                    .setText(workEntry.getActHrs());


            TableRow bodyRownext = (TableRow) LayoutInflater.from(this).inflate(
                    R.id.tableRow3, null);

            ((TextView) bodyRownext.findViewById(R.id.attrib_value3))
                    .setText(workEntry.getActivity());

            ((TextView) bodyRownext.findViewById(R.id.attrib_value4))
                    .setText(workEntry.getWorkEntryDesc());

            table.addView(nestedtable);


        }
        Log.d(TAG, "Load Entries");
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, e.toString());
    }
}

以前は、4 つのエントリすべてを 1 つずつ取得するために繰り返し呼び出した xml ファイルに「tablerow」しかないときに機能していました。しかし、画面スペースを節約するために、2行の「tableyalout」を挿入しようとしましたが、機能しなくなりました。ここで何が間違っていますか?どんな助けでも大歓迎です。

4

2 に答える 2

1

好奇心からこのコードを試してみてください。

public void fillWorkEntries(ArrayList<WorkEntry> workEntries) {     
    try {         
        TableLayout table = (TableLayout)WorkEntryScreenActivity.this.findViewById(R.id.resultDetail);          

        //table.removeAllViews();
        for (WorkEntry workEntry : workEntries) {
            TableLayout nestedtable = (TableLayout) LayoutInflater.from(this).inflate(R.layout.tabrow, null);
            TableRow bodyRow = (TableRow)nestedtablefindViewById(R.id.tableRow2, null);
            ((TextView) nestedtable.findViewById(R.id.attrib_value2)).setText(workEntry.getWorkRequestName());            
            ((TextView) bodyRow.findViewById(R.id.attrib_value5)).setText(workEntry.getActHrs());
            //TableRow bodyRownext = (TableRow) LayoutInflater.from(this).inflate(R.id.tableRow3, null);
            ((TextView) nestedtable.findViewById(R.id.attrib_value3)).setText(workEntry.getActivity());
            ((TextView) nestedtable.findViewById(R.id.attrib_value4)).setText(workEntry.getWorkEntryDesc());

            table.addView(nestedtable);
        }

        Log.d(TAG, "Load Entries");
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, e.toString());
    } 
} 

インフレートしたネストされたテーブル オブジェクトの一部であるため、行をインフレートする必要はありません。

于 2012-06-08T21:02:07.997 に答える
0

Viewレイアウトの親R.layout.tabrowは aであり、キャストすると aTableLayoutではないため、次の行:TableRow

TableRow bodyRow = (TableRow) LayoutInflater.from(this).inflate(
                    R.layout.tabrow, null);

キャスト例外をスローします。代わりに、次のようにする必要があります。

TableLayout bodyRow = (TableLayout) LayoutInflater.from(this).inflate(
                    R.layout.tabrow, null);
((TextView) bodyRow.findViewById(R.id.attrib_value2))
                .setText(workEntry.getWorkRequestName());
((TextView) bodyRow.findViewById(R.id.attrib_value5))
                .setText(workEntry.getActHrs());
((TextView) bodyRownext.findViewById(R.id.attrib_value3))
                .setText(workEntry.getActivity());
((TextView) bodyRownext.findViewById(R.id.attrib_value4))
                .setText(workEntry.getWorkEntryDesc());
TableRow tr = new TableRow(this);
TableRow.LayoutParams p = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
bodyRow.setlayoutParams(p);
tr.addView(bodyRow);
TableLayout.LayoutParams p1 = new TableTableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(p1);
table.addView(tr);
于 2012-06-08T19:26:58.563 に答える