DAVIDBALAS1
DAVIDBALAS1

Reputation: 484

Setting views as global variables android

I have three functions in my Main class, onCreate,onClick and click, I have two options, declare TextViews and some other views at the beginning of the project as global variables, which means they will stay in the entire app lifetime, or get them separately in each function(will cause the computer to work a bit more but the variables will not stay in memory the whole time). To describe the question further:

OPTION 1:

public static final int L = 6;
TextView[] textViews = new TextView[L];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Random rand = new Random();
    int[] digits = {rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10)};
    for (int i = 0; i < L; i++) {
        textViews[i] = (TextView) findViewById(getResources().getIdentifier("num" + String.valueOf(i), "id", getPackageName()));
        textViews[i].setOnClickListener(this);
        textViews[i].setText(String.valueOf(digits[i]));
    }
}

@Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.num0:
                click(0);
                break;
            case R.id.num1:
                click(1);
                break;
            case R.id.num2:
                click(2);
                break;
            case R.id.num3:
                click(3);
                break;
            case R.id.num4:
                click(4);
                break;
            case R.id.num5:
                click(5);
                break;
        }
    }


 public void click(int clicked) {
      textViews[clicked].setText("Clicked");       
 }

OPTION 2: (Note to shorten your time - the change is in the last function).

public static final int L = 6;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Random rand = new Random();
    int[] digits = {rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10)};
    for (int i = 0; i < L; i++) {
        textViews[i] = (TextView) findViewById(getResources().getIdentifier("num" + String.valueOf(i), "id", getPackageName()));
        textViews[i].setOnClickListener(this);
        textViews[i].setText(String.valueOf(digits[i]));
    }
}

@Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.num0:
                click(0);
                break;
            case R.id.num1:
                click(1);
                break;
            case R.id.num2:
                click(2);
                break;
            case R.id.num3:
                click(3);
                break;
            case R.id.num4:
                click(4);
                break;
            case R.id.num5:
                click(5);
                break;
        }
    }


 public void click(int clicked) {
      ((TextView) findViewById(getResources().getIdentifier("num" + String.valueOf(clicked),"id",getPackageName())).setText("Clicked");       
 }

Take into consideration I have more than a TextView array, and in some function I have to get 5 views again, will it be better to declare them as global variables? I read somewhere most of the time using global variables is not good and it is against the whole idea of functions, but it seems more simple here.. Sorry about my English and thank you all.

Upvotes: 0

Views: 166

Answers (1)

Gurupad Mamadapur
Gurupad Mamadapur

Reputation: 989

First off, you do not need to worry about performance issues if the textViews object is declared globally. It is minuscule.

Now in your code,if the only reason to declare textViews array object is to reference it in click() method, then there is no need of declaring it globally.

Instead, you can pass the view object provided by the overridden onClick() method to your click() method.

@Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.num0:
                click(view);
                break;
            case R.id.num1:
                click(view);
                break;
            case R.id.num2:
                click(view);
                break;
            case R.id.num3:
                click(view);
                break;
            case R.id.num4:
                click(view);
                break;
            case R.id.num5:
                click(view);
                break;
        }
    }


 public void click(View viewClicked) {
      ((TextView))viewClicked.setText("Clicked")
 }

Upvotes: 1

Related Questions