Harsh
Harsh

Reputation: 487

How to display a list of entries programmatically using TableLayout in android

I'm using a web service to fetch data that is to be shown as a list if entries. I have four attributes which are in a 2X2 table layout format in my xml file. Then I use TableLayout class in my java code to store the result in each TableRow.

Here's my xml code: 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>

Here's the another xml code inside which the tabrow.xml will be called to add each entry.

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

And following is the java code.

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

It used to work before when I have only a 'tablerow' in my xml file which I iteratively called to fetch all four entries one by one. But to save some screen space I tried to insert a 'tableyalout' with two rows and it stopped working. What am I doing wrong here? Any kinda help is appreciated.

Upvotes: 0

Views: 1833

Answers (2)

trumpetlicks
trumpetlicks

Reputation: 7085

Try this code out of curiosity.

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

You shouldnt need to inflate your rows, as they are already part of the nested table object you have inflated

Upvotes: 1

user
user

Reputation: 87064

The parent View of the layout R.layout.tabrow is a TableLayout and not a TableRow as you cast it so this line:

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

will throw a cast exception. Instead it should be like this:

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

Upvotes: 0

Related Questions