Call method with View parameter in another method

Asked

Viewed 717 times

0

Examples of a method that requires a View

public void lista(View v) {
      Toast.makeText(this, "Ok", Toast.LENGTH_SHORT).show();
}

public void botaoAbrir (View view) { 
      Intent i = new Intent(this, NovoRegistro.class); 
      startActivity(i); 
}

I’d like to call them inside onCreate() or any other method that does not require View.

The way I tried and worked:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lista(new View(this));
}

But the point is, it’s right ?

It can be used like this without problems ? What other ways ?

It’s to my advantage to create a View to the class, and always call her ?

2 answers

1

I suppose this method is used by a Button whose click Listener has been assigned, in its xml declaration, with android:onClick=lista.

It seems that you want this method to be called not only when you click the button but also by your code.

In such situations it is customary to have a private method with the code you want to be executed in both situations. This method is called by the method lista() when the button is clicked and directly in the other case(your code).

It would be something like that:

public void lista(View v) {
      doListaClick();
}

private void doListaClick(){
    Toast.makeText(this, "Ok", Toast.LENGTH_SHORT).show();
}

In the onCreate() would be so:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        doListaClick();
}

As you see it is not necessary to use or "invent" any view to execute the code you want.

  • Understood ramaral ! But this process of separating the 2 methods would be good practice, better performance, security ? Or could call with the "null" doListaClick(null); ?

  • 1

    Better performance? Nothing relevant. Best practice? I think so. Pass null as argument usually denotes "gambiarra". null is (maybe) the worst error in computer science. The very one Tony Hoare apologizes because of that.

0


Let’s think a little... If the method requires a View as a parameter, it is because the processing performed in this method is inherent to View that will be passed on.

How do we know a View is an element that is on the app screen (read What is a View on Android?).

Therefore, probably this method that calls for this View makes changes to the application screen or even to the View passed. Following this logic, instantiate an object View is not correct, since it will only be instantiated and will not exist properly on the application screen. If the View is not required for the method to work then it makes no sense to pass it by parameter.

The correct approach is to take this View on the application screen and pass it on to the method using the method findViewById and take the element of the screen that refers to this method.

To catch the screen element, use the id which was used in the creation of the XML of Activity, below an example of how this should be done:

View viewNaTela = findViewById(R.id.elementoNaTela);
lista(viewNaTela);
  • Could post as pegar o elemento da tela que é referente à este método. ? If I do it in the method, because in the onCreate the Activity View will already exist ?

  • Edited response.

  • I edited the question, the method requires having the View, for example Toast. It’s not that it requires elements, but the call of a view object.

  • Toast does not require a View and yes a Context see Toast documentation: https://developer.android.com/reference/android/widget/Toast.html, you can see that in the example you posted the various v is not used at any time

  • I get it. And in the case of a Button which calls the method ? If the method does not have the View, closes the application...

  • I don’t understand your question, I created the chat room: https://chat.stackexchange.com/rooms/72546/room-for-julio-borges-and-rbz. we will continue there

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.