开发者

Android Spinner getting selectedItem Value?

I have setup Spinner with the layout below, and I wanted to get the value of the item selected, not the displayed text. Where does one pull out the value? Also will this work for pulling out values the layout below? Or do I need some other way to setup (value, displayText) pair? So I guess I need to know how to set it up so it has a value and also how in onItemSelected I would pull the value?

<?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="@arra开发者_Python百科y/some_values" 
android:prompt="@array/some_names"/>
</LinearLayout>


You resolve the Spinner instance in your activity class with findViewById and set an OnItemSelectedListener on it. Since you are populating the Spinner with some kind of Adapter, use the items that you constructed the Adapter with and the position of the selected item as reported back in the third parameter of OnItemSelectedListener's onItemSelected method

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


I hope it will helpful to you.

Try this Code..

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

            }

        });




    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜