In my android app, I have a Spinner, and I try to detect when an item is selected. I know this can be done using setOnItemSelectedListener()
method, but what I don't understand is that this works fine:
((Spinner)findViewById(R.Id.mySpinner)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
// implement onItemClick here
});
While this gives an error that the parameter is "not applicable" to Spinner:
((Spinner)findViewById(R.Id.mySpinner)).setOnItemSelectedListener(new MyOnItemSelectedListener());
Where MyOnItemSelectedListener is this, and it's a nested class in my MainActivity:
private class MyOnItemSelectedListener implements AdapterView.OnItemSelectedListener
{
public MyOnItemSelectedListener() {}
// implement onItemClick here
}
What I want to achieve is not just code that works - I already have that. It's code that's easily readable (with the real name of the things in my code and the structure of my MainActivity class, it is more readable using a nested class than an anonymous one).
How I understand java, these two should be the same thing essentially except one class has a name, and the other one doesn't.
In your code, You have used
Id
(R.Id.mySpinner
) instead ofid
(R.id.mySpinner
).Both implementation in your code will definitely work.
Initialize Spinner:
Inner Class:
This is actually working fine for me. Check with your
id
andlayout
.