2016-02-16 88 views
-1

這是我的代碼。我有一個帶有奇數行的文件。最後還是留下一個空白按鈕。如果我將其更改爲偶數行,它工作正常。 我希望它能讀取每行的行數並添加一個按鈕,而不管是否存在偶數或奇數行。 我在做什麼錯。 謝謝,while語句仍然添加空白行,即使是false?

 // This builds the number of rows based on the number of lines in the file. 
     while (line != null){ 

      TableRow tblRow = new TableRow(this); 
      tblRow.setPadding(5, 30, 5, 5); 
      table.addView(tblRow); 

      // iterates through the number of lines. 
      // filling each cell with a button with the name of each part in the file. 
      for (int col = 0; col < NUM_COL; col++) { 
       Button btn = new Button(this); 
       btn.setTextSize(14); 
       btn.setText(line); 
       btn.setOnClickListener(this); 
       tblRow.addView(btn); 
       line = reader.readLine(); 
      } 
     }; 
+1

你調試你的代碼? – Reimeus

+0

我嘗試了一個catch,但沒有在IOException中顯示。如果我將閱讀器移出閱讀器,它就會永遠循環。 – vbneil54

+0

這是我在調試模式下得到的東西 - 無法打開調試器端口(localhost:8624):java.net.ConnectException「連接被拒絕:連接」 – vbneil54

回答

0

更改這個

while (line != null){ 

     TableRow tblRow = new TableRow(this); 
     tblRow.setPadding(5, 30, 5, 5); 
     table.addView(tblRow); 

     for (int col = 0; col < NUM_COL; col++) { 
      if (line != null) { 
       //Normal flow 
       Button btn = new Button(this); 
       btn.setTextSize(14); 
       btn.setText(line); 
       btn.setOnClickListener(this); 
       tblRow.addView(btn); 
       line = reader.readLine(); 
      }else{ 
       //Guaranteed to happen on odd number of lines since NUM_COL=2 
       //Might want to add a dummy view here if the missing button affects the table display 

       //NOP 

      } 

     } 
} 
+0

感謝您的嘗試。有兩件事發生,現在什麼都沒有出現。其次,如果我在for語句之前添加table.addview並將其留在else中,則會得到一行兩列。如果我將它從其他人中刪除,我會得到該行上所有按鈕的一行。但我認爲我們正在接近。 – vbneil54

+0

其餘的你應該保留 –

+0

好吧,就像你說的那樣再次運行它。這次我得到一個錯誤。 - 指定的孩子已經有一位家長。您必須先調用子對象的父對象的removeView()。 - 我迷路了。我認爲這是因爲table.addview在while時被首先調用,然後再在else中當該行爲空時。 – vbneil54

相關問題