Spinner data to string

核能气质少年 提交于 2019-12-24 11:27:28

问题


I have a spinner bounded with data, and I want to select it and put the string selected in a variable..

How can I do this?


回答1:


You can use getSelectedItem to get the currently selected item. If you've bound to an ArrayAdapter<String>, this will be the value.

Hope this helps,

Phil Lello




回答2:


Assuming the data is a plain String,

  mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String str = (String) parent.getItemAtPosition (pos); // Your string
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // your code here, if any 
    }
}


来源:https://stackoverflow.com/questions/5651402/spinner-data-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!