0

In this code below can i only once instantiate view? Then, I can using it for some method in helper class.

public class Helper {

    private Context context;
    private Activity activity;

    private TextView textView;
    private Button button;

    public Helper(Context context, Activity activity) {
        this.context = context;
        this.activity = activity;
    }

    public void firstMethod() {
        textView = (TextView) activity.findViewById(R.id.text_view);
        button = (Button) activity.findViewById(R.id.button);

        textView.setText(R.string.some_text_1);
        button.setText("Tes1 Button")
    }

    public void secondMethod() {
        textView = (TextView) activity.findViewById(R.id.text_view);
        button = (Button) activity.findViewById(R.id.button);

        textView.setText(R.string.some_text_2);
        button.setText("Tes2 Button")
    }
}

As you can see textView and button instantiate twice in different method, but I just want single instantiate. How to code that?

UPDATE

In activity we can do this:

protected void onCreate(Bundle savedInstanceState) {
    //see this code
    textView = (TextView) findViewById(R.id.text_view);
    button = (Button) findViewById(R.id.button);
}

And then in method we just do like this:

public void someMethod() {
        textView.setText(R.string.some_text_2);
        button.setText("Tes2 Button")
}

I need code like shown above in non activity. is it possible ?

3 Answers3

0

Use context instead of activity:-

Make sure you need to pass Activity to Non Activity class as constructor parameter as you are initialized in your code.

refer:-findViewById in non activity class

   public void secondMethod() {
        textView = (TextView) context.findViewById(R.id.text_view);
        button = (Button) context.findViewById(R.id.button);

        textView.setText(R.string.some_text_2);
        button.setText("Tes2 Button")
    }
Mr. Roshan
  • 1,777
  • 13
  • 33
0

If I understand what you're asking correctly, you can instantiate them in your constructor, like this

public Helper(Context context) {
    // You really don't even need to save context in this case since
    // you only use it in the constructor. If your real problem is more
    // complex you may still need to though
    this.context = context;

    textView = (TextView) context.findViewById(R.id.text_view);
    button = (Button) context.findViewById(R.id.button);
}

then you don't have to do so in firstMethod and secondMethod, which can just be

public void firstMethod() {
    textView.setText(R.string.some_text_1);
    button.setText("Tes1 Button")
}

public void secondMethod() {
    textView.setText(R.string.some_text_2);
    button.setText("Tes2 Button")
}

You could then create your helper class from within, say, your Activity onCreate like

private Helper helper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_time_till);
    helper = new Helper(this);
}
Tyler V
  • 9,694
  • 3
  • 26
  • 52
  • Thanks a lot sir, you solved my problem. I have tried put them in my constructor before but my app crash, because I init helper class in outside of onCreate, this cause the app crash. – user-56729194712 Jul 07 '18 at 23:27
  • You're welcome. You can't call findViewById before you've called setContentView, which is probably why it didn't work for you before. – Tyler V Jul 07 '18 at 23:51
0

Make initialization in constructor:

public Helper(Context context, Activity activity) {
    this.context = context;
    this.activity = activity;

    textView = (TextView) activity.findViewById(R.id.text_view);
    button = (Button) activity.findViewById(R.id.button);
}