Why doesn't the Spinner class offer generics?

As explained in what-is-a-raw-type-and-why-shouldnt-we-use-it , using raw classes is evil.

Now Spinner .getSelectedItem()

returns an object in which it could return the correct class if it was initialized Spinner<CorrectClass>

.

Why is this so?

+3


source to share


1 answer


getSelectedItem()

is the method AdapterView

that returns Adapter#getItem()

. So the question is, "Why isn't the interface adapter shared?"

The answer is simply because the Android AdapterView widget can be used to store various types of objects. The purpose of the adapter is to provide a consistent view of these objects.

If you add a common option in the interface Adapter

(and AdapterView

and Spinner

), you will record the type of baseline data collection, which goes against the whole adapter thing.



The idea is that the adapter view doesn't need to know about the underlying data behind the adapter, so it doesn't need to restrict its data type.

However, you can specialize an interface Adapter

with a specific type (or generic segment) in your own implementation SpinnerAdapter

, and then access the data through an adapter, not an AdapterView:

yourAdapter.getItem(spinner.getSelectedItemPosition()).

      

+2


source







All Articles