Android Spinner OnItemSelectedListener not working accurately

五迷三道 提交于 2019-12-10 18:26:12

问题


I have created a Spinner and activated the listener:

customerListSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
    public void onItemSelected(AdapterView<?> adapter, View view, int position, long id) {
        // I do some work here
    }
    public void onNothingSelected(AdapterView<?> arg0) {
        isInitRadioGroup = false;
    }
 );

I have added three values in the Spinner initially: "Search", "Employee", "Company". If the user selects "Search", a new ListActivity is shown and the selected value is added to the above Spinner as a result.
Like this, I am performing some action on the selection of the spinner Item. Now, When the screen is shown at first time, by default "Search" is shown. To trigger the processing of the ListActivity, the user has to select the "Search" again. This time, on ItemSelected callback is not called. I mean to say, if a value in Spinner is already selected, then on the selection of the same value again, does not trigger the listener.

Whereas: When the screen is shown at first time, by default "Search" is shown. The user selects the value "Employee" (processing for this is done, onItemSelected is called as the previous selected value is different than the value selected by the user). The user then selects the value "Search". This time the onItemSelected method is called and successfully adds a new value to the Spinner.

This is a bug of Android. I have checked there is no error in my code.


回答1:


Try to use the spinner as following

Spinner genspin=(Spinner)findViewById(R.id.gender);

ArrayAdapter<String> genadap=new ArrayAdapter(this,R.layout.spinneritems,String_array);
genadap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
genspin.setAdapter(genadap);
genspin.setOnItemSelectedListener(this);

then you can get the selected value as

genspin.getSelectedItem().toString()


来源:https://stackoverflow.com/questions/6788664/android-spinner-onitemselectedlistener-not-working-accurately

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