0

second day in android self teachin and saw this code bleow. from what I understood, it seems to me that the code is getting the button value

final Button GetServerData = (Button) findViewById(R.id.GetServerData);

and then I am not sure what happened. Being from php background this syntax looks very unfamiliar that a method is being called as a methods parameter in here

GetServerData.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // WebServer Request URL
                String serverURL = "http://androidexample.com/media/webservice/JsonReturn.php";

                // Use AsyncTask execute Method To Prevent ANR Problem
                new LongOperation().execute(serverURL);
            }
        });    

I also am not sure what View arg0 is.

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.rest_ful_webservice);  

    final Button GetServerData = (Button) findViewById(R.id.GetServerData);

    GetServerData.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // WebServer Request URL
            String serverURL = "http://androidexample.com/media/webservice/JsonReturn.php";

            // Use AsyncTask execute Method To Prevent ANR Problem
            new LongOperation().execute(serverURL);
        }
    });    

}
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221

1 Answers1

2

1) This is type casting, the method
findViewById returns something, the method
which called it casts the result to Button.

2) This an anonymous class, this is a class
implementing an interface, the class is defined
right there at the place of its usage.

3) The OnClickListener interface
apparently has one method called
onClick and it has one View argument.
This is what arg0 is. But it does not
seem to be used in the implementing class.
The name arg0 is not really important.
You can name it also x or y or anything else.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159