I am trying to add a bunch of rows to an empty Table during execution. I have tried using some test code but for some reason the screen stays empty. If I had elements in the .XML They do show up, but not the table itself, unless I add some rows/elements inside the .XML file as well.
I would really appreciate some help.
My class extends Activity.
Here is the code to update the Table:
public Runnable UpdateUserList = new Runnable() {
public void run() {
TableLayout userTable = (TableLayout) findViewById(R.id.listOfUsers);
for (String user : userNames) {
TableRow row = new TableRow(getBaseContext());
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView field = new TextView(getBaseContext());
field.setText(user);
field.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
row.addView(field);
userTable.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
}
};
I call the code using a Handler this way, from a Thread:
mHandler.post(UpdateUserList);
The code does get run and does not print any error.
This is the XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:background="@drawable/main_bg"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_marginTop="10px" android:gravity="center_horizontal"
android:layout_weight="1" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="@string/userList"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="25px" />
</LinearLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/listOfUsers">
</TableLayout>
</LinearLayout>
Any help would be very useful. I am very stuck right now.
EDIT: Using ListView
private Runnable UpdateUserList = new Runnable() {
public void run() {
ListView userTable = (ListView) findViewById(R.id.listOfUsers);
userTable.setAdapter(new ArrayAdapter<String>(getBaseContext(),
R.layout.connectserver, userNames));
}
};
And the XML:
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/listOfUsers" />
Rather than the TableLayout.