开发者

Android spinner prompt not working [duplicate]

This question already has answers here: How to make an Android Spinner with initial text "Select One"? 开发者_StackOverflow社区 (36 answers) Closed 10 years ago.

I have Spinners that I am using in my application. They are working fine with one exception. I have set prompts for each one, but they are not showing. I am setting ArrayAdapters to the Spinners during onCreate, and my guess is that the setAdapter method is automatically setting the selection to position 0. Is there a way to set the prompt and have it work as expected?

Here is a code piece:

From the layout file:

<Spinner android:id="@+id/selPunter"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:prompt="@string/select_quarterback_prompt"
         android:layout_marginLeft="20sp"
         android:layout_marginRight="20sp" />

From activity:

offenseList = new ArrayAdapter<PlayerVO>(this,
                                         R.layout.select_item_closed,
                                         gdm.getPlayersByTeamId(offenseId));
offenseList.setDropDownViewResource(R.layout.select_item);
selKicker.setAdapter(offenseList);


This seems to happen even if you put the OnItemSelectedListener in the onStart() method of the activity.

The work around I did for this issue was I put a default message in position 0 of my resource array ("Select Trip Type"). So when the OnItemSelectedListener is called, if position 0 is selected, then do nothing. Here is my code:

 mTripTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> parent,View v,int position,long rowId) {
            //boolean used for hiding spinner
            boolean hideSpinner = true;

            switch(position){
                case 0:
                    //nothing was selected - defualt "Select Trip Type"
                    hideSpinner = false;
                    break;
                case 1:
                    mCurrentStop.setStopType(Stop.STOP_TYPE.DELIVERY);
                    break;
                case 2:
                    mCurrentStop.setStopType(Stop.STOP_TYPE.START_OF_BREAK);
                    break;
                case 3:
                    mCurrentStop.setStopType(Stop.STOP_TYPE.END_OF_BREAK);
                    break;
                case 4:
                    mCurrentStop.setStopType(Stop.STOP_TYPE.START_OF_LUNCH);
                    break;
                case 5:
                    mCurrentStop.setStopType(Stop.STOP_TYPE.END_OF_LUNCH);
                    break;
                case 6:
                    mCurrentStop.setStopType(Stop.STOP_TYPE.END_OF_TRIP);
                    break;
            }

            //display other data screens
            displayData(hideSpinner);
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            mCurrentStop.setStopType(Stop.STOP_TYPE.DELIVERY);
        }
    });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜