2014-04-02 31 views
0

我目前正在爲一個項目開發我的第一個應用程序,並想知道應該使用對象並且代碼少或者代碼多但沒有對象。 這裏是代碼將進入4個不同的方法(我每個活動)或一個類在每個活動中引用。以上這Android開發:是否在使用對象不好的做法?

 TextView text1 = (TextView)this.cal.findViewById(R.id.txt1); 
     TextView text2 = (TextView)this.cal.findViewById(R.id.txt2); 
     TextView text3 = (TextView)this.cal.findViewById(R.id.txt3); 
     TextView text4 = (TextView)this.cal.findViewById(R.id.txt4); 
     TextView text5 = (TextView)this.cal.findViewById(R.id.txt5); 
     TextView text6 = (TextView)this.cal.findViewById(R.id.txt6); 
     TextView text7 = (TextView)this.cal.findViewById(R.id.txt7); 
     TextView text8 = (TextView)this.cal.findViewById(R.id.txt8); 
     TextView text9 = (TextView)this.cal.findViewById(R.id.txt9); 
     TextView text10 = (TextView)this.cal.findViewById(R.id.txt10); 
     TextView text11 = (TextView)this.cal.findViewById(R.id.txt11); 
     if(x == 0) 
     { 
      text1.setText (text1.getText() + "sciemce and enginnering"); 
      text2.setText (text2.getText() + "add a bit"); 
      text3.setText (text3.getText() + "add a bit"); 
      text4.setText (text4.getText() + "add a bit"); 
      text5.setText (text5.getText() + "add a bit"); 
      text6.setText (text6.getText() + "add a bit"); 
      text7.setText (text7.getText() + "add a bit"); 
      text8.setText (text8.getText() + "add a bit"); 
      text9.setText (text9.getText() + "add a bit"); 
      text10.setText (text10.getText() + "add a bit"); 
      text11.setText(text11.getText() + "add a bit"); 

     } 
     else if(x ==1) 
     { 
      text1.setText (text1.getText() + "arts"); 
      text2.setText (text2.getText() + "add a bit"); 
      text3.setText (text3.getText() + "add a bit"); 
      text4.setText (text4.getText() + "add a bit"); 
      text5.setText (text5.getText() + "add a bit"); 
      text6.setText (text6.getText() + "add a bit"); 
      text7.setText (text7.getText() + "add a bit"); 
      text8.setText (text8.getText() + "add a bit"); 
      text9.setText (text9.getText() + "add a bit"); 
      text10.setText (text10.getText() + "add a bit"); 
      text11.setText(text11.getText() + "add a bit"); 
     } 

     else if(x == 2) 
     { 
      text1.setText (text1.getText() + "1"); 
      text2.setText (text2.getText() + "add a bit"); 
      text3.setText (text3.getText() + "add a bit"); 
      text4.setText (text4.getText() + "add a bit"); 
      text5.setText (text5.getText() + "add a bit"); 
      text6.setText (text6.getText() + "add a bit"); 
      text7.setText (text7.getText() + "add a bit"); 
      text8.setText (text8.getText() + "add a bit"); 
      text9.setText (text9.getText() + "add a bit"); 
      text10.setText (text10.getText() + "add a bit"); 
      text11.setText(text11.getText() + "add a bit"); 

     } 

     else if(x ==3) 
     { 
      text1.setText (text1.getText() + "1"); 
      text2.setText (text2.getText() + "add a bit"); 
      text3.setText (text3.getText() + "add a bit"); 
      text4.setText (text4.getText() + "add a bit"); 
      text5.setText (text5.getText() + "add a bit"); 
      text6.setText (text6.getText() + "add a bit"); 
      text7.setText (text7.getText() + "add a bit"); 
      text8.setText (text8.getText() + "add a bit"); 
      text9.setText (text9.getText() + "add a bit"); 
      text10.setText (text10.getText() + "add a bit"); 
      text11.setText(text11.getText() + "add a bit"); 
     } 





} 

這段代碼很長(正好爲4 if語句)將出現在4個不同的活動,我加載它的變化取決於一個教師分級制度。 我現在在一個單獨的類中,我創建了一個類的對象來調用該方法來加載上面的代碼的表,或者我應該把這些代碼分開放在我的活動中,因爲我聽說使用了ojects不好的做法。 非常感謝,如果它非常模糊,但它太多的代碼張貼=)感到抱歉。

+0

使用循環嘗試,這將節省alots重複。 – Shang

+0

「使用對象」通常是一個非常好的主意 - 它有助於保持以邏輯和可管理的方式分組的特定任務的代碼 - 但這不是您需要的;你需要一個數組。 – chrylis

+1

不要低估這個人,他提出了一個合法的問題。每個人都是初學者。 – rupps

回答

3

您可以用循環簡化它,例如:

else if(x ==1) 
    { 
     text1.setText (text1.getText() + "arts"); 
     text2.setText (text2.getText() + "add a bit"); 
     text3.setText (text3.getText() + "add a bit"); 
     text4.setText (text4.getText() + "add a bit"); 
     text5.setText (text5.getText() + "add a bit"); 
     text6.setText (text6.getText() + "add a bit"); 
     text7.setText (text7.getText() + "add a bit"); 
     text8.setText (text8.getText() + "add a bit"); 
     text9.setText (text9.getText() + "add a bit"); 
     text10.setText (text10.getText() + "add a bit"); 
     text11.setText(text11.getText() + "add a bit"); 
    } 

for (int i = 1; i < 12; i++){ 
    text[i].setText(....) 
} 
+0

哦,我知道如何使用for循環,但「添加一點」隨每個文本視圖的變化sp texx1可能是藝術,文字兩可能是科學等.... – UnholySalmon

0

有很多的事情可以做,在這裏。跳出來的第一件事是:x的價值是什麼意思?當你檢查x == 0時,那個條件代表什麼?我會將「x」重命名爲更具描述性的內容,並且可能會使用具有硬編碼整數的命名常量。所以,你的代碼會是這樣

if (selectedDegree == SCIENCE_AND_ENGINEERING)

,而不是

if (x == 0)

和任何人閱讀你的代碼會更明白。我也會創建一個輔助方法來每次追加文本。

text1.setText (text1.getText() + "sciemce and enginnering"); 
text2.setText (text2.getText() + "add a bit"); 
text3.setText (text3.getText() + "add a bit"); 
text4.setText (text4.getText() + "add a bit"); 

將成爲

appendText(text1, "science and engineering"); 
appendText(text2, "and a bit"); 
appendText(text3, "and a bit"); 
appendText(text4, "and a bit"); 

private void appendText(TextView textView, String addedText) { 
    if (textView != null) { 
     textView.setText(textView.getText() + addedText); 
    } 
} 
+0

啊,這很好,非常感謝很多=)。 – UnholySalmon

0
  1. 的表?如果在id上沒有太多的工作基礎,請嘗試使用List,而不是像這樣的很多對象。

    list.add(this.cal.findViewById(...)) 
    
  2. 並改變if ... else ..切換;

  3. 使用循環完全正常。

    for(item in list){ 
        if(x==1 && index==0) {item.setText("...");} 
        else{item.setText("another...");} 
    } 
    
相關問題