简体   繁体   中英

Adding multiple TextViews into LinearLayout dynamically

I have a problem with adding TextViews dynamically. 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.

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:

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

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. If you want to use multiple TextViews then change your loop to

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":

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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