簡體   English   中英

將TextView或TextView數組傳遞給Android中的函數

[英]Passing a TextView or TextView Array to a function in Android

我是一個菜鳥程序員,我試圖建立一個函數來傳遞一個TextViewarrayTextViews的功能,所以它可以在不同的點被稱為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中顯示錯誤,但是該程序不喜歡這兩種情況。

通常,將它們傳遞給功能不是一個好主意...

但如果您願意,可以嘗試類似的方法...

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

最好使用WeakReference對象的數組來保存對您的Text視圖的引用,並在活動的OnCreate方法中初始化此數組,每次系統破壞您的活動時,都可以使用此方法自動對項目的引用進行垃圾回收,並且您不會造成內存泄漏。 據我所知,保持對短暫對象(如活動本身或活動視圖)的引用的標准方法是使用WeakReference對象。 為了了解什么是WeakReference,請閱讀以下Wikipedia文章:

http://en.wikipedia.org/wiki/弱引用

另請閱讀此頁面:

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

您可以嘗試使用postStats(TextView... VIEWSTAT)來代替Overloading postStats()函數。 在此函數中,您可以通過VIEWSTAT.length檢查參數的VIEWSTAT.length

        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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM