2014-04-02 46 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...");} 
    } 
    
相关问题