Spinner中的setOnItemSelectedListener没有调用

时间:2012-08-23 11:40:11

标签: android button spinner

我有一个添加项目的方法:

timezoneEdit.setOnClickListener(new OnClickListener() {
        public void onClick(View v)
        {
             timezonSpin = (Spinner) findViewById(R.id.timezoneSpin);//timezoneCall();  
           final ArrayList <String> timezonelist = new ArrayList<String>();

            timezonelist.add("Gvatemala");
            timezonelist.add("Gonduras");
            timezonelist.add("Batumi");
            timezonelist.add("Boston");
            timezonelist.add("Zhytomir");
            timezonelist.add("Zmerinka");
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(NewOrderActivity.this,
            android.R.layout.simple_spinner_item, timezonelist);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            timezonSpin.setAdapter(dataAdapter);timezonSpin.performClick(); 
            timezonSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
                {

                    Log.i("sd","Ya tut");
                    Log.i("choosed",timezonelist.get(position).toString());
                    timezoneEdit.setText(timezonelist.get(position));
                }

                public void onNothingSelected(AdapterView<?> arg0) {

                    TimeZone timezone = TimeZone.getDefault();
                    String TimeZoneName = timezone.getDisplayName();
                    int TimeZoneOffset = timezone.getRawOffset()/(60 * 60 * 1000);
                    Log.i("timezone",Integer.toString(TimeZoneOffset));
                    timezoneEdit.setText("UTC"+Integer.toString(TimeZoneOffset), TextView.BufferType.EDITABLE);

                }
         });

当用户点击按钮时,微调器正在显示,用户可以选择其中一个项目。但奇怪的是onItemSelectedListener没有调用。我使用这样的shema用于我的其他常规旋转器(没有按钮点击使用)并且一切正常在那里...缺少什么?

2 个答案:

答案 0 :(得分:1)

timezoneEdit.setOnClickListener(new OnClickListener() {
        public void onClick(View v)
        {
             Spinner timezonSpin = (Spinner) findViewById(R.id.timezoneSpin);//timezoneCall();  
           final ArrayList <String> timezonelist = new ArrayList<String>();

            timezonelist.add("Gvatemala");
            timezonelist.add("Gonduras");
            timezonelist.add("Batumi");
            timezonelist.add("Boston");
            timezonelist.add("Zhytomir");
            timezonelist.add("Zmerinka");
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(NewOrderActivity.this,
            android.R.layout.simple_spinner_item, timezonelist);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            timezonSpin.setAdapter(dataAdapter);timezonSpin.performClick(); 
            timezonSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
                {

                    Log.i("sd","Ya tut");
                    Log.i("choosed",timezonelist.get(position).toString());
                    timezoneEdit.setText(timezonelist.get(position));
                }

                public void onNothingSelected(AdapterView<?> arg0) {

                    TimeZone timezone = TimeZone.getDefault();
                    String TimeZoneName = timezone.getDisplayName();
                    int TimeZoneOffset = timezone.getRawOffset()/(60 * 60 * 1000);
                    Log.i("timezone",Integer.toString(TimeZoneOffset));
                    timezoneEdit.setText("UTC"+Integer.toString(TimeZoneOffset), TextView.BufferType.EDITABLE);

                }
         });

尝试上面的代码。我认为你必须在setOnClickListener事件中制作微调器对象。

答案 1 :(得分:0)

将timezonSpinner.setOnItemSelectedListener移出块timezoneEdit.setOnClickListener(new OnClickListener(){}