2011-07-22 174 views
-1

有沒有辦法動態更改變量?例如,這就是我想要做的。Android:動態更改變量

我有這樣的:

TextView tv = new TextView(this); 

這是一個do while循環。我想要做的是每次循環時改變實際的變量電視,所以我不會繼續在前面的文本視圖上書寫。

我嘗試了各種方法,但沒有任何工作。

+1

你的問題對我來說沒有任何意義。請解釋你正在努力完成的任務,更重要的是,請展示你正在討論的那個循環的代碼。 –

回答

0

我猜你希望能夠引用循環結尾處的所有TextViews,但是你只能訪問最後一個生成的?如果是這樣,請在每次迭代中創建一個TextViews數組,並將新的TextView添加到數組中。這樣,你就可以通過它的數組索引訪問每一個。

0

您應該創建一個TextViews數組,我假設在循環結束時(或在循環過程中)您要將所有TextView添加到您的佈局。此代碼假定你想在循環過程中添加textviews,你知道多少次迭代while循環將執行

LinearLayout myTextViewLayout = (LinearLayout) findViewById(R.id.Linear_Layout_Name_In_Xml); 
int iterations = THE_NUMBER_OF_ITERATIONS_YOUR_WHILE_LOOP_WILL_PERFORM; 
TextView []myTextViews = new TextView[iterations]; //Array of TextViews to use as references 
int i = 0; 
while(your_condition){ 
TextView textView = new TextView(); //declare a new TextView 
textView.setText(YOUR_TEXT_HERE); 
myTextViewLayout.addView(textView); //add textView to your UI 
myTextViews[i] = textView; //save Reference to your new textView in an Array 
i++; 
} 

現在您可以訪問那些textViews您的陣列英寸