onItemSelected / setOnItemSelectedListener未触发

时间:2012-06-12 06:59:30

标签: android

  
    

我打开spiiner with onclick on button但是我的微调器不是     触发setOnItemSelectedListener这段代码不工作public void onItemSelected(AdapterView arg0,View arg1,     int pos,                                     长id){                                 language = AppConstants.Language_arr [pos];

  
                    }
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    } spinner opens but onitemselected is not working
import com.privy.constants.AppConstants;
            import android.app.Activity;
            import android.os.Bundle;
            import android.view.View;
            import android.view.View.OnClickListener;
            import android.widget.AdapterView;
            import android.widget.AdapterView.OnItemSelectedListener;
            import android.widget.ArrayAdapter;
            import android.widget.Button;
            import android.widget.Spinner;
            import android.widget.ToggleButton;

            public class Settings extends Activity implements OnClickListener {
                private Spinner sp_languge, sp_login;
                private ToggleButton toggle_news_letter;
                private String language;
                private Button language_change;
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    // TODO Auto-generated method stub
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.settings);
                    sp_languge = new Spinner(this);
                    ArrayAdapter<String> adapter_language = new ArrayAdapter<String>(
                            Settings.this, R.layout.spinner_item, AppConstants.Language_arr);
                    sp_languge.setAdapter(adapter_language);
                    language_change = (Button) findViewById(R.id.language_change);
                    language_change.setOnClickListener(this);
                    sp_languge.setOnItemSelectedListener(new OnItemSelectedListener() {

                        public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
                                long id) {
                            language = AppConstants.Language_arr[pos];

                        }
                        public void onNothingSelected(AdapterView<?> arg0) {
                            // TODO Auto-generated method stub

                        }
                    });
                }
                public void onClick(View v) {
                    if (v == language_change) {

                        sp_languge.performClick();

                    }
                }
            }

1 个答案:

答案 0 :(得分:1)

在XMl上添加微调器,宽度/高度为零...................

并使用

sp_languge = (Spinner)findViewById(R.id.<my_spinerid>);

android spinner performClick onItemSelected