我有一个Spinner,我正在使用setAdapter更改每个项目点击的条目。我面临的问题是,在点击微调器时,无限期地调用setOnItemSelectedListener。您可以在下面的代码中看到,单击每个项目时将显示一个确认框,该对话框只会显示无限次。 以下是我的代码
adapter_after_confirm = ArrayAdapter.createFromResource(this,
R.array.spinner_after_confirmation, android.R.layout.simple_spinner_item);
adapter_after_waiting = ArrayAdapter.createFromResource(this,
R.array.spinner_after_waiting, android.R.layout.simple_spinner_item);
adapter_after_boarded = ArrayAdapter.createFromResource(this,
R.array.spinner_after_boarded, android.R.layout.simple_spinner_item);
adapter_after_reached = ArrayAdapter.createFromResource(this,
R.array.spinner_after_reached, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter_after_confirm.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter_after_waiting.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter_after_boarded.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter_after_reached.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String selected = parent.getItemAtPosition(position).toString();
if(spinnerSelected){
if(selected.equals("Waiting For Cab")){
confirmDialog("Waiting For Cab");
spinnerStatusChange.setAdapter(adapter_after_waiting);
}
else if(selected.equals("Boarded Cab")){
confirmDialog("Boarded Cab");
spinnerStatusChange.setAdapter(adapter_after_boarded);
}
else if(selected.equals("Reached Destination")){
confirmDialog("Boarded Cab");
spinnerStatusChange.setAdapter(adapter_after_reached);
spinnerStatusChange.setEnabled(false);
Toast.makeText(getApplicationContext(),
"Location tracking is now disabled",
Toast.LENGTH_SHORT).show();
}
}
}
});
答案 0 :(得分:0)
您不必使用“if(spinnerSelected)”,因为仅在选择微调器时调用methot。
试试这个:
adapter_after_confirm = ArrayAdapter.createFromResource(this,
R.array.spinner_after_confirmation, android.R.layout.simple_spinner_item);
adapter_after_waiting = ArrayAdapter.createFromResource(this,
R.array.spinner_after_waiting, android.R.layout.simple_spinner_item);
adapter_after_boarded = ArrayAdapter.createFromResource(this,
R.array.spinner_after_boarded, android.R.layout.simple_spinner_item);
adapter_after_reached = ArrayAdapter.createFromResource(this,
R.array.spinner_after_reached, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter_after_confirm.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter_after_waiting.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter_after_boarded.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter_after_reached.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String selected = parent.getItemAtPosition(position).toString();
if(selected.equals("Waiting For Cab")){
confirmDialog("Waiting For Cab");
spinnerStatusChange.setAdapter(adapter_after_waiting);
}
else if(selected.equals("Boarded Cab")){
confirmDialog("Boarded Cab");
spinnerStatusChange.setAdapter(adapter_after_boarded);
}
else if(selected.equals("Reached Destination")){
confirmDialog("Boarded Cab");
spinnerStatusChange.setAdapter(adapter_after_reached);
spinnerStatusChange.setEnabled(false);
Toast.makeText(getApplicationContext(),
"Location tracking is now disabled",
Toast.LENGTH_SHORT).show();
}
}
});