Android spinner prompt not working [duplicate]

╄→гoц情女王★ 提交于 2019-12-11 08:05:16

问题


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);

回答1:


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);
        }
    });


来源:https://stackoverflow.com/questions/7314685/android-spinner-prompt-not-working

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