Android Spinner获得selectedItem值?

时间:2011-02-26 01:36:31

标签: android spinner

我已经设置了Spinner,其布局如下,我想获取所选项目的值,而不是显示的文本。人们在哪里取出价值?这也可以用于提取下面布局的值吗?或者我是否需要一些其他方式来设置(value,displayText)对?所以我想我需要知道如何设置它以便它有一个值,以及如何在onItemSelected我会拉取值?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingLeft="12dip"> 
<Spinner
android:id="@+id/viewSpin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"   
android:entries="@array/some_values" 
android:prompt="@array/some_names"/>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

使用findViewById解析活动类中的Spinner实例,并在其上设置OnItemSelectedListener。由于您使用某种适配器填充Spinner,因此请使用您构造适配器的项目以及OnItemSelectedListener的onItemSelected方法的第三个参数中报告的所选项目的位置

http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html#onItemSelected%28android.widget.AdapterView%3C?%3E,%20android.view.View,%20int,%20long%29

答案 1 :(得分:1)

我希望它对你有所帮助。

试试此代码..

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final List<String> list=new ArrayList<String>();
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");
        list.add("Item 4");
        list.add("Item 5");

        final String[] str={"Report 1","Report 2","Report 3","Report 4","Report 5"};

        final Spinner sp1= (Spinner) findViewById(R.id.spinner1);
        final Spinner sp2= (Spinner) findViewById(R.id.spinner2);
        final Spinner sp3= (Spinner) findViewById(R.id.spinner3);

        ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,list);
        adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp1.setAdapter(adp1);

        ArrayAdapter<String> adp2=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,str);
        adp2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp2.setAdapter(adp2);
        sp2.setSelection(adp2.getPosition("Report 3"));

        ArrayAdapter<CharSequence> adp3=ArrayAdapter.createFromResource(this, 
                R.array.str2, android.R.layout.simple_spinner_item);

        adp3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp3.setAdapter(adp3);


        sp1.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long id) {
                // TODO Auto-generated method stub
                int pos1=position;
                String str1=sp1.getSelectedItem().toString();
                //Toast.makeText(getBaseContext(), list.get(position), Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });


        sp2.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long id) {
                // TODO Auto-generated method stub
                int selected_item_position = arg2;
                String selected_item=sp2.getSelectedItem().toString();

                Toast.makeText(getBaseContext(), ""+selected_item_position, Toast.LENGTH_SHORT).show();
                Toast.makeText(getBaseContext(), selected_item, Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });


        sp3.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long id) {
                // TODO Auto-generated method stub
                int pos1=position;
                String str1=sp1.getSelectedItem().toString();               




            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });




    }