在我的代码中,当您在微调器中选择一个项目时,应用程序会更改屏幕。但是,当它第一次加载时,它会执行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
}
});
}
答案 0 :(得分:3)
我通过在OnItemSelectedListener中添加previousSelection
之类的变量来解决这个问题:
null
,那就是误报。这是我对类似问题的回答,我写了这个问题来避免你的问题以及当用户再次选择同一个项目时。 Odd Android Spinner behavior