开发者

Android Spinner OnItemSelectedListener not working accurately

开发者 https://www.devze.com 2023-03-22 09:49 出处:网络
I have created a Spinner and activated the listener: customerListSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){

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", "Com开发者_JAVA技巧pany". 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.


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()
0

精彩评论

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

关注公众号