0

我有这样一种添加项目的方法:

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用于我的其他常规微调器(无需单击按钮),并且那里一切正常……缺少什么?

4

2 回答 2

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 事件中创建微调器对象。

于 2012-08-23T11:48:59.703 回答
0

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

于 2012-08-23T11:47:21.610 回答