0
在我的代码中,当您在微调器中选择一个项目时,应用程序会更改屏幕。但是,当它第一次加载时,它会执行ItemSelectedListener。我只需要在Spinner中选择一个项目时执行代码,而不是在窗体加载时执行代码。Android微调器在OnCreate中执行ItemSelectedListener
代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_location_layout);
Spinner spnEUID = (Spinner)findViewById(R.id.spnEUID);
final DatabaseHandler handler = new DatabaseHandler(this);
ArrayList<String> EUIDs = handler.GetAllAOI();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, EUIDs);
spnEUID.setAdapter(adapter);
final EditText txtEUID = (EditText)findViewById(R.id.txtScanEUID);
txtEUID.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
PublicVariables.AOI = handler.getAOI(Integer.parseInt(txtEUID.getText().toString()));
Intent intent = new Intent(SelectLocationScreen.this, RaploScanScreen.class);
startActivity(intent);
}
return false;
}
});
spnEUID.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
PublicVariables.AOI = parent.getItemAtPosition(pos).toString();
Intent intent = new Intent(SelectLocationScreen.this, RaploScanScreen.class);
startActivity(intent);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
谢谢。我曾尝试过类似的东西。我的错误是我声明变量的地方。再次感谢! – plasmy 2013-03-15 12:41:33