What type of object does Spinner.getItemAtPosition (...) return?

What will return this operator?

parent.getItemAtPosition(position) 

Where parent is the parent view for the counter, and position is the selected position from the counter view.

+4
source share
2 answers

I assume that the "parent" you are talking about is Spinner. In this case:

 Spinner.getItemAtPosition(pos); 

will always return the type of object that you filled in Spinner,.

Example of using CustomType: (Spinner is filled with elements of type "CustomType", so getItemAtPosition (...) returns CustomType)

 Spinner spinner = (Spinner) findViewById(R.id.spinner1); CustomType [] customArray = new CustomType[] { .... your custom items here .... }; // fill an arrayadapter and set it to the spinner ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, customArray); spinner.setAdapter(adapter); CustomType type = (CustomType) spinner.getItemAtPosition(0); // it will return your CustomType so you can safely cast to it 

Another example of using String Array: (Spinner is filled with elements of type String), so getItemAtPosition (...) will return String)

 Spinner spinner = (Spinner) findViewById(R.id.spinner1); String[] stringArray= new String[] { "A", "B", "C" }; // fill an arrayadapter and set it to the spinner ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stringArray); spinner.setAdapter(adapter); String item = (String ) spinner.getItemAtPosition(0); // it will return your String so you can safely cast to it 
+5
source

It will return the dataType object that you are showing in the spinner.

suppose you show a String array, then it will return a string.

if you show an array of Integer, it returns Integer, etc.

+1
source

Source: https://habr.com/ru/post/1497569/


All Articles