2012-04-13 41 views
-2

我相對來說是一個noobie,並且一直在尋找進出來試圖弄清楚我的代碼出了什麼問題。這是一個基本的列表視圖將被分解成子表的無法訪問的代碼在循環中

ListView localListView = (ListView)findViewById(2131361899); 
    localListView.setAdapter(new ArrayAdapter(this, 2130903117, arrayOfString)); 
    localListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    { 
//Start// 
     public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) 
     { 
     String str = ((TextView)paramView).getText().toString(); 
     if (str.equalsIgnoreCase(ChapterList.this.getResources().getString(2131230721))) 
      ChapterList.this.startActivity(new Intent(ChapterList.this, Foreword.class)); 
     while (true) 
     {return; 
     //The Error starts here// 
     if (str.equalsIgnoreCase(ChapterList.this.getResources().getString(2131230723))) 
     { 
      ChapterList.this.startActivity(new Intent(ChapterList.this, Chapter1Section.class)); 
      continue; 
     } 
      //And ends here// 
     if (str.equalsIgnoreCase(ChapterList.this.getResources().getString(2131230728))) 
     { 
      ChapterList.this.startActivity(new Intent(ChapterList.this, Chapter2Section.class)); 
      continue; 
     } 
     if (str.equalsIgnoreCase(ChapterList.this.getResources().getString(2131230745))) 
     { 
      ChapterList.this.startActivity(new Intent(ChapterList.this, Chapter3Section.class)); 
      continue; 
     } 
     if (str.equalsIgnoreCase(ChapterList.this.getResources().getString(2131230752))) 
     { 
      ChapterList.this.startActivity(new Intent(ChapterList.this, Chapter4Section.class)); 
      continue; 
     } 
     if (str.equalsIgnoreCase(ChapterList.this.getResources().getString(2131230759))) 
     { 
      ChapterList.this.startActivity(new Intent(ChapterList.this, Chapter5Section.class)); 
      continue; 
     } 
     if (str.equalsIgnoreCase(ChapterList.this.getResources().getString(2131230764))) 
     { 
      ChapterList.this.startActivity(new Intent(ChapterList.this, Chapter6Section.class)); 
      continue; 
     } 
     if (str.equalsIgnoreCase(ChapterList.this.getResources().getString(2131230765))) 
     { 
      ChapterList.this.startActivity(new Intent(ChapterList.this, Chapter7Section.class)); 
      continue; 
     } 
     if (str.equalsIgnoreCase(ChapterList.this.getResources().getString(2131230768))) 
     { 
      ChapterList.this.startActivity(new Intent(ChapterList.this, Chapter8Section.class)); 
      continue; 
     } 
     if (str.equalsIgnoreCase(ChapterList.this.getResources().getString(2131230778))) 
     { 
      ChapterList.this.startActivity(new Intent(ChapterList.this, Chapter9Section.class)); 
      continue; 
     } 
     if (!str.equalsIgnoreCase(ChapterList.this.getResources().getString(2131230722))) 
      continue; 
     ChapterList.this.startActivity(new Intent(ChapterList.this, Appendix.class)); 
     } 
     } 
+2

此代碼拼命乞求一些重構。任何時候如果你有很長的代碼塊會重複除了一兩個小點以外的任何事情,你應該考慮一個循環和某種數據結構來列出每次迭代的差異。 – 2012-04-13 18:02:56

+0

當然,但「我怎麼能做這種清潔劑」不是問題。 =]對於初學者來說,在真正理解風格和清潔問題之前,讓他們理解程序流程,作業等的基本知識是他們的先決條件。 – derekv 2012-04-13 18:12:37

回答

1

的代碼return;語句不會到達後。 在while循環塊內部,每次評估一條語句。 但是,當評估語句return;時,整個方法將「調用」控制權返回給調用方法,並且在該方法中不會執行更多代碼。

+0

此外,最後一條if語句的最後一行也會有相同的問題。 '繼續;'將執行的代碼的最後一行,以便它後面的行永遠不會被執行。 – David 2012-04-14 02:44:30