2013-03-20 42 views
0

我在滾動視圖中多次膨脹4個線性佈局。每個佈局在標題中都有一個標題。代碼片段如下。充氣android後動態命名textview

for(int j=0;j<qType.length;j++) 
    {   
     LinearLayout.LayoutParams siz = new LinearLayout.LayoutParams(width, height);; 
     if(qType[j]==0) 
     {    
      view1 = getLayoutInflater().inflate(R.layout.layout1, main_layout,false); 
      siz = new LinearLayout.LayoutParams(width, height/3);          
     } 
     else if(qType[j]==1) 
     {     
      view1 = getLayoutInflater().inflate(R.layout.layout3, main_layout,false); 
      siz = new LinearLayout.LayoutParams(width, height/3); 
     } 
     else if(qType[j]==2) 
     {   
      view1 = getLayoutInflater().inflate(R.layout.layout4, main_layout,false); 
      siz = new LinearLayout.LayoutParams(width, height/3); 
     } 
     else if(qType[j]==3) 
     {   
      view1 = getLayoutInflater().inflate(R.layout.layout5, main_layout,false); 
      siz = new LinearLayout.LayoutParams(width, height/2); 
     }  

     siz.topMargin = 25; 
     main_layout.addView(view1, siz);    
    } 
    scroll_layout.addView(main_layout); 
    scroll_layout.setBackgroundResource(R.drawable.options_background);  
    setContentView(scroll_layout); 

現在每個佈局中都有textview,我想更改它的文本。如果我通過findviewbyid訪問它們並給出settext,則只有第一個實例正在更改,我想在所有場合更改textview。

for(int k=0;k<NumberQuestions.length;k++) 
    { 
     TextView number_title = (TextView)main_layout.findViewById(R.id.number_title); 
     TextView mcq_title = (TextView)main_layout.findViewById(R.id.mcq_title); 
     TextView comment_title = (TextView)main_layout.findViewById(R.id.comment_title); 
     TextView cam_title = (TextView)main_layout.findViewById(R.id.cam_title); 
     if(qType[k]==0) 
     {    
      number_title.setTypeface(myriadpro_bold); 
      number_title.setText(NumberQuestions[k]); 

     } 
     if(qType[k]==1) 
     { 
      comment_title.setTypeface(myriadpro_bold); 
      comment_title.setText(NumberQuestions[k]);    
     } 
     else if(qType[k]==2) 
     { 
      mcq_title.setTypeface(myriadpro_bold); 
      mcq_title.setText(NumberQuestions[k]);    
     } 
     else if(qType[k]==3) 
     { 
      cam_title.setTypeface(myriadpro_bold); 
      cam_title.setText(NumberQuestions[k]);    
     }   

請幫忙。

+0

您指的是什麼'TextView',你在哪裏調用'setText()'? – codeMagic 2013-03-20 17:38:45

+0

請粘貼您的textview代碼 – 2013-03-20 17:41:25

回答

0

聲明:我覺得您正在嘗試對付Android Framework,而不是使用它。我的建議有點徹底的改變,但我確實認爲這是正確的方式去做事情。

我會推薦使用ListView,因爲你重複多次相同的格式。一個列表視圖通常會有一個對象對象的容器,以及一個負責將對象映射到列表視圖中的特定行的適配器。當您更改底層容器中的數據時,您將調用適配器的回調notifyDataSetChanged(),以通知它您更改了底層數據並提醒其更新您的列表視圖。

2大資源/從Android開發者網站教程: 的ListView:http://developer.android.com/guide/topics/ui/layout/listview.html 適配器:http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

0

可能TextView的android: id在所有佈局都是一樣的,從而解釋findViewById始終返回第一個方法的行爲發生

0

如果我是正確main_layout是在XML命名視圖file.So你應該使用這樣

view1 = getLayoutInflater().inflate(R.layout.layout1, main_layout,false); 
TextView tv = (TextView)main_layout.findViewById(R.id.tv1); 

其中電視是layout1.xml中的texview