Spinner.getItemAtPosition(...)返回哪种类型的对象? [英] What type of Object does Spinner.getItemAtPosition(...) return?
问题描述
此语句将返回什么?
parent.getItemAtPosition(position)
parent
是微调器的父视图,而position是从微调器视图中选择的位置.
Where parent
is a parent view for a spinner and position is the selected position from the spinner view.
推荐答案
我假设您正在谈论的父母"是Spinner.在这种情况下:
I assume the "parent" you are talking about is a Spinner. In this case:
Spinner.getItemAtPosition(pos);
将始终返回填充了Spinner的对象的类型.
使用CustomType的示例:(微调器中填充了"CustomType"类型的项目,因此getItemAtPosition(...)将返回CustomType)
An example using a CustomType: (the Spinner is filled with Items of type "CustomType", therefore getItemAtPosition(...) will return 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
另一个使用字符串数组的示例:(微调器中填充了字符串"类型的项目,因此getItemAtPosition(...)将返回字符串)
Another example using a String Array: (the Spinner is filled with Items of type "String", therefore 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
这篇关于Spinner.getItemAtPosition(...)返回哪种类型的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!