3

This has been asked before, but I have found no solution that worked for me yet:

When adding rows to a table from code, the rows are not showing up in the application. There is one row I specified in XML, that one is showing up, but nothing below it.

This is the code, I've added the constructor of my class as well:

private LayoutInflater theInflater;
private LinkedList<View> rows;
private Context thisContext;

public DistanceTableView(LayoutInflater vi, Context context)
{
  theInflater = vi;
  rows = new LinkedList<View>();
  thisContext = context;    
}

public void addRow(LocationMessage locationMsg){
  View messageView = theInflater.inflate(R.layout.homepage, null);
  TableLayout table = (TableLayout)messageView.findViewById(R.id.distanceTable);

  TextView senderNameTextView = new TextView(thisContext);
  senderNameTextView.setText(locationMsg.getSenderName());

  TableRow tr = new TableRow(thisContext);
  tr.addView(distanceTextView);
  table.addView(tr);
  rows.addFirst(messageView);
}

homepage.xml contains this, I removed some elements and parameters:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
    <TabHost>
    <TabWidget />
        <FrameLayout>
        [..]        
            <LinearLayout>          
            [..]
                <TableLayout
                android:id="@+id/distanceTable" 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_gravity="center"
                android:background="#DDDDDD"
                android:stretchColumns="1" >
                    <TableRow>
                        <TextView
                        android:textColor="#000000"
                        android:text="@string/label_device"
                        android:layout_gravity="center"
                        android:padding="3dip"
                        android:textSize="18sp" />
                        <TextView
                        android:textColor="#000000"
                        android:text="@string/label_distance"
                        android:layout_gravity="center"
                        android:padding="3dip"
                        android:textSize="18sp" />
                        <TextView
                        android:textColor="#000000"
                        android:text="@string/label_time"
                        android:layout_gravity="center"
                        android:padding="3dip"
                        android:textSize="18sp" />
                    </TableRow>
                </TableLayout>
            </LinearLayout>
        </FrameLayout>
    </TabHost>
</LinearLayout>

Unfortunately hierarchyviewer.bat doesn't work for me in order to check if the rows are there but just not visible. In the debugger it looks fine to me.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lars
  • 523
  • 1
  • 6
  • 20
  • Is your `addRow()` method in your `Activity`? What is `rows` - how is it initialised and what does `addFirst()` do? How is `thisContext` initialised? – dave.c Jan 31 '11 at 16:16
  • I've extended the code part in my question above. `rows` is a `LinkedList` of Views. I've added the constructor initializing the stuff. `addRow()` is in a class that extends another class which extends a `MapActivity`. – Lars Feb 01 '11 at 10:05

1 Answers1

0

I think you should call TableLayout.requestLayout() after you've added the new row.

From the docs:

Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119