1

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?

4 Answers 4

2

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);
    }
}
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.