简体   繁体   中英

Spinner only saving first word to string

I am trying to get the spinner to display the item which i have selected. But it is only displaying the first word even if i choose the ones below. Here is the code i am using

ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
            getApplicationContext(), R.layout.spinner_item, R.id.textView1, al);

spFacilityType.setAdapter(aa1);

spFacilityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        int index = arg0.getSelectedItemPosition();
        position = index;
    }
});

final String Strspinner = spFacility.getItemAtPosition(position).toString();

使用onItemSelected而不是onNothingSelected。

do the code in onItemSelected()..

String s= spFacilityType.getSelectedItem().toString();

now s will show the selected item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM