Rows added to the table are not displayed.

This was asked earlier, but I did not find a solution that worked for me yet:

When adding rows to the table from the code, the rows are not displayed in the application. There is one line specified in XML that is displayed, but nothing below.

This is the code, I added the constructor of my class:

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, the hierarchyviewer.bat file does not work for me to check if rows exist, but just don't appear. In the debugger, it looks good.

+3
source share
1 answer

I think you should call TableLayout.requestLayout () after adding a new row.

From docs :

, - . .

0

Source: https://habr.com/ru/post/1789012/


All Articles