【问题标题】:Passing a TextView or TextView Array to a function in Android将 TextView 或 TextView 数组传递给 Android 中的函数
【发布时间】:2013-07-12 05:39:34
【问题描述】:

我是一名新手程序员,我正在尝试设置一个函数以将 TextViewsTextViewarray 传递给函数,因此可以在 @987654324 中的各个点调用它@。

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 中显示错误,但程序并不喜欢这两种情况。

【问题讨论】:

  • 你为什么不把它们做成字段?

标签: java android arrays textview


【解决方案1】:

通常将它们传递给函数不是一个好主意...

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

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]);
 }
}

【讨论】:

    【解决方案2】:

    最好使用 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

    【讨论】:

      【解决方案3】:

      您可以尝试使用postStats(TextView... VIEWSTAT),而不是Overloading postStats() 函数。在这个函数中你可以通过VIEWSTAT.length查看参数的数量。

      【讨论】:

        【解决方案4】:
                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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多