2011-07-08 101 views
0

我正在尋找在一個屏幕上創建兩個區域,從動態tablerows/tablelayout生成Android TableLayout - 「指定的孩子已經有了父母,你必須先調用孩子父母的removeView()。」

第一個tablelayout將有1個表和6個textview元素。

第二個tablelayout將有2個表中每個6個按鈕。

每個Button/textview都是從xml文件中購買的。

問題我試圖運行這兩個表時,我正在拋出標題中指出的錯誤。在下一行

tablerow.addView(View1); 

CODE

Layout1 = (TableLayout) findViewById(R.id.scoring_tableLayout1); 
Layout2 = (TableLayout) findViewById(R.id.scoring_tableLayout2); 

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

text = new TextView[6]; 
TableRow tablerow1 = new TableRow(this); 

for (int i = 0; i < 6; i++) { 
    View1 = (View) inflater.inflate(R.layout.layout1, null); 
    text[i] = (TextView) endValueView.findViewById(R.id.textView_item); 
    tablerow.addView(View1); 
} 
Layout1.addView(tablerow1); 

button = new Button[12]; 

for (int i = 0; i < 2; i++) { 
    TableRow tablerow2 = new TableRow(this); 
    for (int j = 0; j < 6; j++) { 
     ButtonView = (View) inflater.inflate(R.layout.button, null); 
     button[j] = (Button) ButtonView 
       .findViewById(R.id.scoring_arrow_score_button); 
     button[j].setText("some text"); 
     tablerow2.addView(ButtonView); 
    } 
    Layout2.addView(tablerow2); 
} 

感謝。它錯誤您的時間

回答

0

我想這是因爲您呼叫的循環中相同textView_item。

text [i] =(TextView)endValueView.findViewById(R.id.textView_item);

你如何命名你的textView和ButtonView?

理想情況下,你可以做的是設置每個textVeiw和ButtonView爲tvItem1,tvItem2..etc的ID .....

,並請到這個link,在這裏你會發現如何在循環內部找到ViewById。

+0

我打電話在第二個循環內的同一個id按鈕,並且工作。它對文本瀏覽器的工作方式是不同的 –

0

當你膨脹一個組件時,你會自動將它添加到正在執行代碼的視圖中。例如, 。如果你有類似的東西:

public calss MyClass extends LinearLayout { 
... 
private void init() { 
    inflater.inflate(R.layout.button, null); 
    ... 

你會將可視組件(按鈕)添加到LinearLayout(MyClass)中。 這就是爲什麼當你循環並使同一個按鈕膨脹兩次時,錯誤表示組件已被添加。

你真正想使用的是findViewById而不是你使用的充氣器。

祝你好運!

相關問題