我搜索了一個解決方案,以避免在動態創建的微調器上自動選擇。我發現鏈接:Spinner onItemSelected() executes when it is not suppose toOn Spinner init - >自動選擇避免
但我不能讓它在我的代碼工作:
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();
}
在類型AbsSpinner的方法爲setSelection(INT)是不適用的參數(字符串) – hevilp 2015-02-05 17:00:36
當你拿到項目的數組列表然後插入一個空白項目在你自己的任何位置和setSelection(yourCustomaddedposition)。 – Shadik 2015-02-05 17:08:18
而在適配器中,您可以將if((item!= null)&&(position == yourcustomaddedposition))的條件放入row.setVisibility(View.GONE);其他{ } } – Shadik 2015-02-05 17:09:19