在执行期间向空 TableLayout 添加行

发布于 2024-10-06 06:01:08 字数 2612 浏览 4 评论 0原文

我试图在执行期间将一堆行添加到空表中。我尝试使用一些测试代码,但由于某种原因屏幕保持空白。如果我在 .XML 中有元素,它们确实会显示,但不会显示表本身,除非我也在 .XML 文件中添加一些行/元素。

我真的很感激一些帮助。

我的课程扩展了“活动”。

下面是更新表的代码:

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

我以这种方式从线程中使用处理程序调用代码:

mHandler.post(UpdateUserList);  

代码确实运行并且不打印任何错误。

这是 XML 文件:

<?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>

任何帮助都会非常有用。我现在很困难。

编辑:使用 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));
    }
};

和 XML:

<ListView android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:id="@+id/listOfUsers" />

而不是 TableLayout。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

述情 2024-10-13 06:01:08

我想您会发现使用 List 比 TableLayout 更容易。其一是为了容纳多个项目而构建的,并且可以轻松地与 java.util.List 或 Array 集成。其次也是更重要的是,如果您拥有的项目多于可见的项目,那么内存效率会更高。

对于列表,它仅实例化可见行的视图 + 1 的 N 列表。如果您使用 TableLayout 来显示相同​​的列表,它会实例化 N 个视图。因此,随着列表的增长,TableLayout 的内存使用量也会增加。列表则不然。无论该列表中有多少项目,它都是一样的。

此外,它提供了更好的方法来跟踪用户单击某个项目时选择了哪些项目。 TableLayout 对此不提供任何帮助,因此您必须推出自己的机制来进行选择。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文