Android - spinner - setOnItemSelectedListener如何从arrayList中获取所选对象?

时间:2017-03-27 13:52:28

标签: android

我使用带有适配器的微调器,它从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) {

            }
        });

1 个答案:

答案 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());