Android Spinner提示不起作用 [英] Android spinner prompt not working

查看:331
本文介绍了Android Spinner提示不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有正在应用程序中使用的微调器.他们工作正常,只有一个例外.我已经为每个提示设置了提示,但是没有显示.我在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);

解决方案

即使您将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);
        }
    });

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

这篇关于Android Spinner提示不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆