2012-05-14 74 views
1

我已經使用sax解析器成功解析了數據,輸出在logcat中正常運行,但我必須系統地安排這些數據。但問題是我的問題長度和索引是2,所以選項也來了2.相反,他們是4每個問題。請建議在android中得到錯誤

代碼段是

for (int i = 0; i < categorylist.getTitle().size(); i++) { 

      TableRow tr = new TableRow(this); 
      tr.setPadding(2, 2, 2, 2); 

      TableLayout.LayoutParams tableRowParams= 
        new TableLayout.LayoutParams 
        (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.FILL_PARENT); 

      int leftMargin=0; 
      int topMargin=2; 
      int rightMargin=0; 
      int bottomMargin=1; 

      tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin); 

      tr.setLayoutParams(tableRowParams); 

      name[i]= new TextView(this); 
      name[i].setText(categorylist.getTitle().get(i)); 
      name[i].setTextColor(Color.WHITE); 
      name[i].setTextSize(12); 
      name[i].setPadding(10,0,0,0); 
      name[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
      tr.addView(name[i]); 
      t1.addView(tr, tableRowParams); 

      LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
        RadioGroup.LayoutParams.WRAP_CONTENT, 
        RadioGroup.LayoutParams.WRAP_CONTENT); 

      rad1[i]= new RadioButton(this); 
      rad1[i].setText(categorylist.getValue().get(i));  
      rad1[i].setTextColor(Color.WHITE); 
      rad1[i].setTextSize(12); 
      rad1[i].setPadding(10,0,0,0); 
      rad1[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
      rgrp.addView(rad1[i], 0, layoutParams); 

     } 

和XML是

<Values> 
<Question> 

<Description><![CDATA[Some Question]]></Description> 

        <Options> 

         <option value="correct"><![CDATA[All of these]]></option> 

         <option value="incorrect"><![CDATA[Ok]]></option> 

         <option value="incorrect"><![CDATA[Fine]]></option> 

         <option value="incorrect"><![CDATA[Good one]]></option> 





        </Options> 

      </Question> 

</Values> 

logcat的輸出是:

05-14 15:22:27.228: INFO/This is the title:(559): Which of the following are called celestial bodies? 
05-14 15:22:27.248: INFO/This is the option:(559): All of these 
05-14 15:22:27.248: INFO/This is the option:(559): Sun 
05-14 15:22:27.248: INFO/This is the option:(559): Planets 
05-14 15:22:27.248: INFO/This is the option:(559): Moon 
05-14 15:22:27.258: INFO/This is the title:(559): In our solar system, _________ planets revolve around the Sun. 
05-14 15:22:27.258: INFO/This is the option:(559): eight 
05-14 15:22:27.258: INFO/This is the option:(559): five 
05-14 15:22:27.258: INFO/This is the option:(559): ten 
05-14 15:22:27.258: INFO/This is the option:(559): nine 
+0

請增加一個完整的logcat輸出到問題 – thepoosh

+0

所以如果你知道每個問題的選項是4然後寫4而不是大小問什麼是 – Nitin

+0

我也試過。但它是拋出錯誤。數組超出限制的異常。 – Naina

回答

2

我想你應該有兩個環例如

int l; 
    for (int i = 0; i < categorylist.getTitle().size(); i++){ 
    for(int j= 0; j < optionlist.size()/categorylist.getTitle().size(); j++){ 
    //add option to question 
l=l++; 
//use l; 
    } 
    } 
+0

是啊現在它獲得所有8個單選按鈕,但重複第二個問題的第一個問題的選項 – Naina

+0

檢查更新並接受答案使用更新,因爲j總是從0開始 – Nitin

+1

好.. Thanx很多:) – Naina