我使用带有适配器的微调器,它从arrayList
获取值Spinner sourcesSpinner = (Spinner) findViewById(R.id.ad1_filter_source_spinner);
List<SO_Item> sources = helper.getItems();
ArrayAdapter<SO_Item> sourcesadapter =
new ArrayAdapter<SO_Item>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, sources);
sourcesadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sourcesSpinner.setAdapter(sourcesadapter);
如何从侦听器中的选定值中获取对象?
sourcesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 0 :(得分:4)
sourcesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SO_Item selectedItem = sources.get(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
或
SO_Item selectedItem = sources.get(sourcesSpinner.getSelectedItemPosition());