Spinner onitemselectedlistener问题

时间:2012-06-27 10:31:44

标签: android

我尝试按下按钮动态创建微调器。

这就是我所做的:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.leave_balance);

spinner = new Spinner(this);
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, list);
    spinner.setAdapter(spinnerArrayAdapter);

Button  b = (Button)findviewbyid(R.id.button1);
b.setonclicklistener(new Onclicklistener){
   public void onClick(View v) {
            spinner.performClick();


        }
 spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Toast.maketext(context,"selectedspinner",Toast.LENGTH_SHORT).show();

        }

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

        }
    });
}
}

我的问题是我无法获得吐司,即旋转器onitemselectedlistner没有触发。

任何建议都将不胜感激..谢谢

3 个答案:

答案 0 :(得分:3)

Spinner需要添加父级才能在onItemSelected

上进行调用
  1. 以XML格式添加sppiner并设置宽度和高度0dp(可设置可见性Invisible未消失)。

  2. 使用findViewById

  3. 获取

答案 1 :(得分:2)

spinner.performClick()调用onclicklistener而不是onItemclicklistener。

使用spinner.performitemclick(.....);

答案 2 :(得分:0)

试试这个。

onCreate()
{

spinnerLocation = (Spinner)findViewById(R.id.spinnerSettingLocation);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.location_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerLocation.setAdapter(adapter);
    spinnerLocation.setOnItemSelectedListener(new MyOnLocationSpinnerItemSelectedListener());

}



public class MyOnLocationSpinnerItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
        Toast.maketext(context,"selectedspinner" + pos,Toast.LENGTH_SHORT).show();

    }




    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
}