简体   繁体   English

动态地将多个TextView添加到LinearLayout

[英]Adding multiple TextViews into LinearLayout dynamically

I have a problem with adding TextViews dynamically. 我在动态添加TextViews时遇到问题。 I want to add hired rooms from list by method getRoomList() at first, and after that add rooms with text ": Free", which are in the existingRoomNames array but are not hired. 我想首先通过方法getRoomList()从列表中添加租用的房间,然后再添加带有文本“:Free”的房间,这些房间位于existingRoomNames getRoomList()数组中但未被租用。

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

Here's the XML: 这是XML:

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

which is inside the parent LinearLayout. 在父级LinearLayout内部。

I get exception like this: 我得到这样的异常:

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

on the last line: 在最后一行:

linearLayout.addView(textView2);

What's the problem there? 那里有什么问题?

You are adding the same view to the layout again. 您将再次向布局添加相同的视图。 Change your function to 将功能更改为

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

Move 移动

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

inside

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

The problem is that you use the same TextView every time. 问题是您每次都使用相同的TextView。 If you want to use multiple TextViews then change your loop to 如果要使用多个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);
    }
}

Please try this -> Make one xml layout file named "text_view": 请尝试->创建一个名为“ text_view”的xml布局文件:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>

then line to solve this problem : 然后行来解决这个问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM