2016-07-22 80 views
1

我在動態添加TextViews時遇到了問題。我想首先通過方法getRoomList()從列表中添加租用的房間,然後添加帶有文本「:Free」的房間,這些房間位於existingRoomNames陣列中,但未被僱傭。將多個TextViews動態添加到LinearLayout中

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

什麼問題呢?

回答

2

你又增加了佈局同樣的觀點。 將您的功能更改爲

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

如果解決了您的問題,請將答案標記爲已接受。 –

+0

解決了,謝謝你! –

1

移動

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

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

問題是您每次都使用相同的TextView。 如果你想使用多個TextViews那麼你的循環改爲

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

請試試這個 - >進行命名的一個XML佈局文件「text_view」:

<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); 
相關問題