Search code examples
javaandroidandroid-adapterview

Why isn't toString() always automatically invoked


So why isn't toString always invoked? This is an example using the Android API.

E.g

@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
        int position, long id) {
    Toast.makeText(this, adapterView, Toast.LENGTH_LONG).show();
}

Will not compile. However if I change it to

@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
            int position, long id) {
    Toast.makeText(this, adapterView.toString(), Toast.LENGTH_LONG).show();
}

It will. What is the actual difference?


Solution

  • What do you mean by always? toString() is just a method which returns a String representation of the object. The Toast.makeText expects a String parameter, but in the first case you give an object of the AdapterView class. So it won't compile:)