简体   繁体   中英

Passing a TextView or TextView Array to a function in Android

I'm a rookie programmer, and I'm trying to setup a function to pass a TextView or an array of TextViews to a function, so it can be called at various points in the 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[]);

I want to have a routine to post the (8) TextViews on my activity_scoreobard layout. I have tried just referencing the STATVIEW1A in the function:

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

I have also tried referencing each of the TextViews from passing an array of TextViews in the function:

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

While both don't show errors in Eclipse, the program does not like either situation.

Generally it's not a good idea to pass them to function...

but if you want you can try something like this...

TextView[] STATVIEW = new TextView[SIZE];

then to initialize

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

then pass your Array

postStats(STATVIEW);

Alternative would be

create a method and pass the values and set them to TextViews

something like this

public void fillData(String[] data)
{
 for (int i=0;i<STATVIEW.length;i++)
 {
    STATVIEW[i].setText(data[i]);
 }
}

It is better to use an array of WeakReference objects to hold the references to your Text views and initialize this array in the OnCreate method of your activity, using this method everytime that system destroy your activity the references to the items can be garbage collected automatically and you won't cause memory leaks. As far as i know the standard way for holding a reference to an ephemeral object like the activity itself or a views on it is to use WeakReference objects. For understanding what a WeakReference is read the following article on Wikipedia:

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

Also read this page:

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

Instead of Overloading postStats() function you can try using postStats(TextView... VIEWSTAT) . In this function you can check the number of arguments by 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM