0

我正在使用 Web 服务来获取要在条目中显示为列表的数据。我的 xml 文件中有四个属性为 2X2 表格布局格式。然后我在我的 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>

这是另一个 xml 代码,其中将调用 tabrow.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());
    }
}

当我的 xml 文件中只有一个“tablerow”时,它曾经工作过,我反复调用它来一一获取所有四个条目。但是为了节省一些屏幕空间,我尝试插入一个两行的“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是 aTableLayout而不是 aTableRow你投射它所以这一行:

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 回答