Spinner得到它的项目值(不是位置)

时间:2014-05-20 10:35:45

标签: java android android-layout android-spinner

我想在微调器中获取所选项的值,而不是项的位置。我怎样才能做到这一点..?如何从微调器中选择餐厅项目时获取BT101(不是位置,不是文本)

这是我的empaccessname.xml文件: -

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="empaccessname">
    <item value="BT101">Restaurant</item>
    <item value="BT102">Saloon</item>
</string-array>
</resources>

这是我的微调代码

spinner1 = (Spinner) findViewById(R.id.empaccessname);

    String text = spinner1.getSelectedItem().toString();

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.empaccessname,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter);
    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            int index = parent.getSelectedItemPosition();
            // storing string resources into Array
            securityques = getResources().getStringArray(
                    R.array.empaccessname);
            bus_type = securityques[index];


        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // do nothing
        }
    });

4 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="empaccessname">
    <item value="BT101">Restaurant</item>
    <item value="BT102">Saloon</item>
</string-array>

<string-array name="empaccesskey">
    <item value="restaurant">BT101</item>
    <item value="saloon">BT102</item>
</string-array>

</resources>

Try to replace this code:
int index = parent.getSelectedItemPosition();
// storing string resources into Array
securityques = getResources().getStringArray(R.array.empaccessname);
bus_type = securityques[index];

With this:
securityques = getResources().getStringArray(R.array.empaccessname);
bus_type = securityques[pos];
securityqueskey = getResources().getStringArray(R.array.empaccesskey);
bus_type_key = securityqueskey[pos];

答案 1 :(得分:0)

您可以执行String.valueOf(spinner1.getSelectedItem()),这将获得String

中当前选定的Spinner

答案 2 :(得分:0)

请使用:

     Object selectedItem = adapter.getItem(pos);
     Log.d(TAG, "Selected item: " + selectedItem);

完整代码:

    final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.empaccessname,
        android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter);
    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
       Object selectedItem = adapter.getItem(pos);
       Log.d(TAG, "Selected item: " + selectedItem);

    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // do nothing
    }
});

答案 3 :(得分:0)

        public class LeadEntry extends Activity implements View.OnClickListener
        {
            Spinner productspinner;

            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.leadentry);
                productspinner = (Spinner) findViewById(R.id.myspinner2);
                productspinner.setOnItemSelectedListener(new OnItemSelectedListener() 
                {
                 @Override
                    public void onItemSelected(AdapterView<?> parent, View view,int position, long id)
                    {
                    //this will give position of item in spinner
                         Toast.makeText(parent.getContext()," OnItemSelectedListener : " parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
                    //   this will give the selected item not position
                         String product = this.productspinner.getSelectedItem().toString();
                    }

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

                    }
                });
        }
        }