在我的代码中,当您在 Spinner 中选择一个项目时,应用程序会更改屏幕。但是,当它第一次加载时,它会执行 ItemSelectedListener。只有当我在 Spinner 中选择一个项目时,我才需要它来执行代码,而不是在加载表单时。
编码:
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
}
});
}