Android 旋转提示不起作用

发布于 2024-12-03 03:04:11 字数 872 浏览 0 评论 0原文

我有在我的应用程序中使用的 Spinners。他们工作得很好,只有一个例外。我已经为每一项设置了提示,但它们没有显示。我在 onCreate 期间将 ArrayAdapters 设置为 Spinners,我的猜测是 setAdapter 方法会自动将选择设置为位置 0。一种设置提示并使其按预期工作的方法?

这是一段代码:

来自布局文件:

<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" />

来自活动:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱果果 2024-12-10 03:04:11

即使您将 OnItemSelectedListener 放在活动的 onStart() 方法中,这种情况似乎也会发生。

我针对此问题所做的解决方法是在资源数组的位置 0 中放置一条默认消息(“选择行程类型”)。因此,当调用 OnItemSelectedListener 时,如果选择了位置 0,则不执行任何操作。这是我的代码:

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

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);
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文