1

我是一名新手程序员,我正在尝试设置一个函数以将 aTextView或 anarray传递TextViews给函数,因此可以在activity.

public class ScoreboardActivity extends Activity {

...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scoreboard);
...       
        final TextView STATVIEW1A = (TextView) findViewById(R.id.place_stat1a); //Team 1
        final TextView STATVIEW1B = (TextView) findViewById(R.id.place_stat1b);
        final TextView STATVIEW1C = (TextView) findViewById(R.id.place_stat1c);
        final TextView STATVIEW1D = (TextView) findViewById(R.id.place_stat1d);
        final TextView STATVIEW2A = (TextView) findViewById(R.id.place_stat2a); //Team 2
        final TextView STATVIEW2B = (TextView) findViewById(R.id.place_stat2b);
        final TextView STATVIEW2C = (TextView) findViewById(R.id.place_stat2c);
        final TextView STATVIEW2D = (TextView) findViewById(R.id.place_stat2d);
        //final TextView[] STATVIEW = {STATVIEW1A,STATVIEW1B,STATVIEW1C,STATVIEW1D,
        //      STATVIEW2A,STATVIEW2B,STATVIEW2C,STATVIEW2D};

...
        postStats();
... or
        postStats(STATVIEW[]);

我想有一个例程在我的 activity_scoreobard 布局上发布 (8) TextViews。我试过在函数中引用 STATVIEW1A:

public void postStats () {
STATVIEW1AX.setText("#dumps: " + Integer.toString(DUMP1[GAME_NO]));
    }

我还尝试通过在函数中传递一组 TextViews 来引用每个 TextViews:

public void postStats (TextView[] VIEWSTAT) {
VIEWSTAT[6].setText("#dumps: "+ Integer.toString(DUMP2[GAME_NO]));
    }

虽然两者都不会在 Eclipse 中显示错误,但程序并不喜欢这两种情况。

4

4 回答 4

0

一般来说,将它们传递给函数并不是一个好主意......

但如果你愿意,你可以尝试这样的事情......

TextView[] STATVIEW = new TextView[SIZE];

然后初始化

 STATVIEW[0] = (TextView) findViewById(R.id.place_stat1a); //Team 1
 STATVIEW[1] = (TextView) findViewById(R.id.place_stat1b);
 ......

然后通过你的Array

postStats(STATVIEW);

替代方案是

创建一个method并传递值并将它们设置为 TextViews

像这样的东西

public void fillData(String[] data)
{
 for (int i=0;i<STATVIEW.length;i++)
 {
    STATVIEW[i].setText(data[i]);
 }
}
于 2013-07-12T05:48:02.120 回答
0

最好使用 Wea​​kReference 对象数组来保存对 Text 视图的引用,并在活动的 OnCreate 方法中初始化此数组,每次系统销毁活动时使用此方法,对项目的引用可以自动进行垃圾收集,并且你不会导致内存泄漏。据我所知,持有对临时对象(如活动本身或对其的看法)的引用的标准方法是使用 Wea​​kReference 对象。要了解什么是 WeakReference,请阅读 Wikipedia 上的以下文章:

http://en.wikipedia.org/wiki/Weak_reference

另请阅读此页面:

https://developer.android.com/reference/java/lang/ref/WeakReference.html

于 2013-07-12T05:48:04.010 回答
0

Overloading postStats()可以尝试使用postStats(TextView... VIEWSTAT). 在此函数中,您可以检查参数的数量VIEWSTAT.length

于 2013-07-12T05:49:04.507 回答
0
        ArrayList<TextView> tmpArrayList = new ArrayList<TextView>();
        for(int y=0; y<10; y++)
        {
            tmpArrayList.add(getTextView(txtBoxCount++));
        }
            this.postStats(tmpArrayList);


private TextView getTextView(int i)
{
    int id=0;

     try {
         id = R.id.class.getField("textview" + i).getInt(0);
        }
     catch (IllegalArgumentException e) {
         e.printStackTrace();
     }
     catch (SecurityException e) {
         e.printStackTrace();
     }
     catch(IllegalAccessException e) {
         e.printStackTrace();
     }
     catch (NoSuchFieldException e) {
         e.printStackTrace();
     }
     textview = (TextView)findViewById(id);
     textview.setTag("0");
     return textview;
}

public void postStats (ArrayList<TextView> viewstat) {
    //do some work here
}
于 2013-07-12T05:53:33.363 回答