2013-08-20 127 views
2

我在android.bt中同時添加了一個靜態和動態文本視圖,但代碼中有錯誤..它只是添加數組的最後一個元素。可以請幫助我來整理一下?在android中同時添加靜態和動態文本視圖

dynamicInput = (String[]) data; 
     runOnUiThread(new Runnable() { 
      public void run() { 

       String[] string = { "Customer Mobile No :", "Nick Name :", 
         "Amount :", "Due date :" }; 
       LinearLayout findViewById = (LinearLayout) findViewById(R.id.dynamicInputbiller); 
       TextView dynamicText = new TextView(PayBiller_Activity.this); 
       TextView staticText = new TextView(PayBiller_Activity.this); 
       for (int i = 0; i < string.length; i++) { 

        staticText.setText(string[i]); 

        staticText.setTextColor(Color.BLACK); 
        staticText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17); 

        dynamicText.setText(dynamicInput[i]); 

        dynamicText.setTextColor(Color.GRAY); 
        dynamicText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13); 
       } 
       findViewById.addView(staticText); 
       findViewById.addView(dynamicText); 

      } 
     }); 
+0

爲什麼你環路(INT I = 0;我

+0

無環路如何添加數組元素在佈局中? – Parinita

+0

好的我發佈我的答案檢查它.. –

回答

0

實際上問題是不同的。你在做什麼是你在單個TextView中添加一個數組元素。正如你在循環中所做的那樣,它只取最後一個元素並在文本視圖中獲取它。做一件事情需要父母佈局的ID(線性/相對)。

在循環

創建一個TextView,並添加一個值給它(你在做這樣的setText方式,setTextSize等)。並在循環內將其添加到其父級佈局。

這可能會對你有所幫助。

+0

當您發佈了答案時,OP會收到通知......無需每次回答問題時發表評論 –

0

試試這個:

dynamicInput = (String[]) data; 
     runOnUiThread(new Runnable() { 
      public void run() { 

       String[] string = { "Customer Mobile No :", "Nick Name :", 
         "Amount :", "Due date :" }; 
       LinearLayout findViewById = (LinearLayout) findViewById(R.id.dynamicInputbiller); 

       for (int i = 0; i < string.length; i++) { 
        TextView dynamicText = new TextView(PayBiller_Activity.this); 
        TextView staticText = new TextView(PayBiller_Activity.this); 
        staticText.setText(string[i]); 

        staticText.setTextColor(Color.BLACK); 
        staticText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17); 

        dynamicText.setText(dynamicInput[i]); 

        dynamicText.setTextColor(Color.GRAY); 
        dynamicText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13); 

        findViewById.addView(staticText); 
        findViewById.addView(dynamicText); 
       } 


      } 
     }); 
+0

感謝您的回覆,但它是崩潰,如果我加入他們內循環。 – Parinita

+0

我得到像這樣的錯誤..... java.lang.IllegalStateException:指定的子項已經有一個父項。您必須先調用子對象的父對象的removeView()。 – Parinita

+0

@Parinita findViewById(dynamicInputbiller)在xml中已經有了子視圖嗎? – nedaRM

0

把這個你在for循環中。

findViewById.addView(staticText); 
findViewById.addView(dynamicText); 
+0

感謝您的答覆,但它是崩潰,如果我加入他們內循環。 – Parinita

+0

你可以發佈你的logcat。 –

+0

我越來越像這樣的錯誤.....java.lang.IllegalStateException:指定的子項已經有父項。您必須先調用子對象的父對象的removeView()。 – Parinita

0

,如果你想將所有的數組元素的textViews則必須將其追加在每次迭代。現在你沒有這樣做。因此你只得到最後一個數組元素。

注意:還要確保您的字符串Array staticInput的字符串數組和staticText的字符串數組具有相同數量的元素,以便您不會使索引超出綁定的異常,或者在將其分配給TextView時可能會遺漏某些元素。

我已經給你的「字符串」數組的其他名稱。其「staticStringArray」。

dynamicInput = (String[]) data; 
    runOnUiThread(new Runnable() { 
     public void run() { 

      String[] staticStringArray= { "Customer Mobile No :", "Nick Name :","Amount :", "Due date :" }; 
      LinearLayout findViewById = (LinearLayout) findViewById(R.id.dynamicInputbiller); 
      TextView dynamicText = new TextView(PayBiller_Activity.this); 
      TextView staticText = new TextView(PayBiller_Activity.this); 
      for (int i = 0; i < staticStringArray.length; i++) { 

       //append each element of the array on each iteration. 
       staticText.setText(staticText.getText().toString() + staticStringArray[i] + "\n"); 

       staticText.setTextColor(Color.BLACK); 
       staticText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17); 

       //append each element of the array on each iteration. 
       dynamicText.setText(dynamicText.getText().toString() + dynamicInput[i] + "\n"); 

       dynamicText.setTextColor(Color.GRAY); 
       dynamicText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13); 
      } 
      findViewById.addView(staticText); 
      findViewById.addView(dynamicText); 

     } 
    }); 
+0

數組元素對於兩者都是相同的..我得到像這樣的錯誤..... java.lang.IllegalStateException:指定的子項已經有一個父項。您必須先調用子對象的父對象的removeView()。 – Parinita

+0

看起來你已經接受了答案。我已經測試了上面的代碼,它的工作原理。那illegalStateException可能是由於其他原因。反正開心編碼。 – SKK