我搜索了一个解决方案,以避免在动态创建的微调器上自动选择。我找到了链接:Spinner onItemSelected() executes when it is not suppose to
但是我无法在我的代码上使用它:
private void populateSpinner() {
//Spinner spinnerKostenstellen;
//spinnerKostenstellen = (Spinner) findViewById(R.id.spinner2);
//spinnerKostenstellen = (Spinner) SpinnerLayout.findViewById(R.id.spinner2);
AlertDialog.Builder adb2 = new AlertDialog.Builder(this);
LayoutInflater adbInflater2 = LayoutInflater.from(this);
View SpinnerLayout = adbInflater2.inflate(R.layout.spinner, null);
adb2.setView(SpinnerLayout);
adb2.setTitle("Kostenstelle auswählen:");
spinnerKostenstellen = (Spinner) SpinnerLayout.findViewById(R.id.spinner2);
spinnerKostenstellen.setSelection(1, false);
spinnerKostenstellen.setOnItemSelectedListener(this);
//spinnerKostenstellen.setCallbackDuringFling(false);
List<String> lables = new ArrayList<String>();
for (int i = 0; i < kostenstellenList.size(); i++) {
lables.add(kostenstellenList.get(i).getKst());
}
// Creating adapter for spinner
//ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
//ArrayAdapter<String> kostenstellenList = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, lables);
// Drop down layout style - list view with radio button
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinnerKostenstellen.setAdapter(spinnerAdapter);
adb2.show();
}
答案 0 :(得分:0)
(EDIT)隐藏了微调器的第一个空白值
List<String> urlist = new ArrayList<String>();
urlist.add(""); // Initial dummy entry
urlist.add("yourstring1");
urlist.add("yourstring2");
urlist.add("yorustring3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, urlist) {
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View v = null;
// If this is the initial blnk entry, make it hidden
if (position == 0) {
TextView tv = new TextView(getContext());
tv.setHeight(0);
tv.setVisibility(View.GONE);
v = tv;
}
else {
// Pass convertView as null to prevent reuse of special case views
v = super.getDropDownView(position, null, parent);
}
return v;
}
};
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(dataAdapter);
答案 1 :(得分:0)
尝试在显示对话框后更改要设置的OnItemSelectedListener
(在设置适配器后也是如此)。这应该可以防止微调器在用户选择之前触发其侦听器。 YOURCLASSNAME
将是此代码所在的当前类的名称。
private void populateSpinner() {
//Spinner spinnerKostenstellen;
//spinnerKostenstellen = (Spinner) findViewById(R.id.spinner2);
//spinnerKostenstellen = (Spinner) SpinnerLayout.findViewById(R.id.spinner2);
AlertDialog.Builder adb2 = new AlertDialog.Builder(this);
LayoutInflater adbInflater2 = LayoutInflater.from(this);
View SpinnerLayout = adbInflater2.inflate(R.layout.spinner, null);
adb2.setView(SpinnerLayout);
adb2.setTitle("Kostenstelle auswählen:");
spinnerKostenstellen = (Spinner) SpinnerLayout.findViewById(R.id.spinner2);
spinnerKostenstellen.setSelection(1, false);
//spinnerKostenstellen.setCallbackDuringFling(false);
List<String> lables = new ArrayList<String>();
for (int i = 0; i < kostenstellenList.size(); i++) {
lables.add(kostenstellenList.get(i).getKst());
}
// Creating adapter for spinner
//ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
//ArrayAdapter<String> kostenstellenList = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, lables);
// Drop down layout style - list view with radio button
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinnerKostenstellen.setAdapter(spinnerAdapter);
// use .create to get the AlertDialog
AlertDialog dialog = adb2.create();
// set an OnShowListener
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
spinnerKostenstellen.setOnItemSelectedListener(YOURCLASSNAME.this);
}
});
dialog.show();
}