1

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.

4

1 回答 1

2

I think you'll find it easier to use a List instead of TableLayout. One it's built to hold multiple items and can be easily integrated with a java.util.List or Array. Secondly and more importantly, it is much more memory efficient if you have more than items than are visible.

With a List it only instantiates views for visible rows + 1 for a list of N. If you use a TableLayout to display the same list it instanties N numbers of views. So as your list grows so does your memory usage with a TableLayout. Not so with a List. It's the same regardless of how many items you have in that list.

Furthermore, it provides better way to track what items were selected when the user clicks on an item. TableLayout doesn't provide any help in this so you have to roll your own mechanism for doing selection.

于 2010-12-08T05:00:43.957 回答