动态添加多个 TextViews 到 LinearLayout

Adding multiple TextViews into LinearLayout dynamically

我在动态添加 TextView 时遇到问题。我想首先通过方法 getRoomList() 从列表中添加已租用的房间,然后添加带有文本“:免费”的房间,这些房间在 existingRoomNames 数组中但未被租用。

public void checkRoomsAndDate() {
    linearLayout = (LinearLayout) findViewById(R.id.linear1);
    linearLayout.removeAllViews();
    for (Room room : mCalendarModel.mList.getRoomList()) {
        addHiredRoomToLayout(room);
    }
    addNotHiredRoomsToLayout();
}

public void addHiredRoomToLayout(Room room) {
    textView = new TextView(this);
    textView.setText(room.getParameters());
    linearLayout.addView(textView);
}

public void addNotHiredRoomsToLayout() {
    textView2 = new TextView(this);
    for (String name : Constants.existingRoomNames) {
        boolean contains = false;
        for (Room room : mCalendarModel.mList.getRoomList()) {
            if (room.getName().equals(name)) {
                contains = true;
            }
        }
        if (!contains) {
            textView2.setText(name + ": Free");
            linearLayout.addView(textView2);
        }
    }
}

这是 XML:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="313dp"
    android:layout_height="150dp"
    android:id="@+id/linear1"
    android:layout_gravity="center"></LinearLayout>

在父 LinearLayout 中。

我遇到这样的异常:

`java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.`

最后一行:

linearLayout.addView(textView2);

那里有什么问题?

移动

linearLayout = (LinearLayout) findViewById(R.id.linear1);
linearLayout.removeAllViews(); 

里面

public void addHiredRoomToLayout(Room room) {
    textView = new TextView(this);
    textView.setText(room.getParameters());
    linearLayout.addView(textView);
}

您再次将相同的视图添加到布局中。 将您的功能更改为

public void addNotHiredRoomsToLayout() {

for (String name : Constants.existingRoomNames) {
    textView2 = new TextView(this);
    boolean contains = false;
    for (Room room : mCalendarModel.mList.getRoomList()) {
        if (room.getName().equals(name)) {
            contains = true;
        }
    }
    if (!contains) {
        textView2.setText(name + ": Free");
        linearLayout.addView(textView2);
    }
}
}

问题是您每次都使用相同的 TextView。 如果您想使用多个 TextView,请将循环更改为

for (String name : Constants.existingRoomNames) {
    textView2 = new TextView(this); //create a new TextView that wasn't added to layout yet

    boolean contains = false;
    for (Room room : mCalendarModel.mList.getRoomList()) {
        if (room.getName().equals(name)) {
            contains = true;
        }
    }
    if (!contains) {
        textView2.setText(name + ": Free");
        linearLayout.addView(textView2);
    }
}

请试试这个 -> 制作一个名为 "text_view":

的 xml 布局文件
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>

然后在线解决这个问题:

textView2 = (TextView) getLayoutInflater().inflate(R.layout.text_view, null);