0
我有Android的問題。我從適配器微調添加數據,然後我設定的選擇上項目的位置0,之後我創建OnItemSelectedListener
,它被觸發,但我不知道爲什麼......選擇後創建的監聽器觸發事件
onCreate方法:
this.spinner = (Spinner)findViewById(R.id.spinner);
this.spinner.setAdapter(MyAdapter.GetAdapter());
this.spinner.setSelection(0);
this.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do some things
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
也許這裏有些不對勁,我也嘗試刪除監聽器,改變選擇,然後添加監聽到它。也許我錯在這裏折騰:
AdapterView.OnItemSelectedListener listener = this.spinner.getOnItemSelectedListener();
this.spinner.setOnItemSelectedListener(null);
this.spinner.setSelection(((SpinnerAdapter) this.spinner.getAdapter()).getElementIndexByTitle(array.get("ProductName")));
listener.onItemSelected(null, null, -1, -1);
this.spinner.setOnItemSelectedListener(listener);
它似乎觸發事件之後也:this.spinner.setOnItemSelectedListener(listener);
也許是有可能不觸發OnItemSelected
選擇項目?
嘗試使用'this.spinner.setSelection(0,假);' –
還是一樣。 –