我目前正在爲我的Android應用程序開展一項帳戶管理活動,但我無法弄清楚爲什麼微調控件的setSelection()方法不會觸發連接到所述Spinner的OnItemSelectedListener 。Spinner.setSelection不會正確觸發OnItemSelectedListener
這是我目前有;
onCreate()方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_management);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
retreiveLanguage();
initializeUI();
// Vérification si l'usager est déjà connecté
Globals appState = ((Globals) this.getApplication());
boolean userLoggedIn = appState.isUserLoggedIn();
boolean userInfoAvailable = appState.isUserInfoAvailable();
if (userLoggedIn && userInfoAvailable) {
fillUI();
}
}
從被稱爲上活動的創建initializeUI()方法相關行其示出了微調的監聽器的結合:
/** OnItemSelectedHandler for the Country Spinner */
mCountrySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Log.i(TAG, "onCountrySelected() was called, position : " + pos);
mProvinces = new ArrayList<String>();
mProvincesCode = new ArrayList<String>();
mXML.parseResponse(FileManager.getInstance().getPortalOptions());
for (int i = 0; i < mXML.getCountry(pos).sizeProvinces(); i++){
mProvinces.add(mXML.getCountry(pos).getProvince(i).getLabel(mLanguage));
mProvincesCode.add(mXML.getCountry(pos).getProvince(i).getCode());
}
mProvinceArrayAdapter = new ArrayAdapter<String>(ManageAccountActivity.this,
android.R.layout.simple_spinner_item, mProvinces);
mProvinceArrayAdapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
mProvinceSpinner.setAdapter(mProvinceArrayAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// Do Nothing ...
}
});
並再次另一條線,這次來自fillUI方法():
Log.i(TAG, "Setting country based on user information.");
((Spinner) findViewById(R.id.spin_country))
.setSelection(mCountriesCode.indexOf(mUser.getCountry()));
// TODO : Fix Provinces and States not being changed accordingly
Log.i(TAG, "Setting province based on user information.");
((Spinner) findViewById(R.id.spin_province))
.setSelection(mProvincesCode.indexOf(mUser.getProvince()));
因此, s我期望OnItemSelectedListener在我在fillUI()方法中設置選擇後立即調用,但這不是在運行時發生的情況:S
這是我的LogCat提取,它顯示當選擇適用於國家/地區Spinner:
I/ManageAccountActivity(28108):根據用戶信息設置國家。
I/ManageAccountActivity(28108):基於用戶信息設置省。
I/ManageAccountActivity(28108):onCountrySelected()被調用,位置:1
作爲一個實驗,我還試圖把fillUI()調用在我的活動的OnStart方法但沒」不會改變應用程序的反應。
預先感謝任何三分球,幫助或提示!
,OnItemSelectedListener只有當你對微調執行一些操作時纔會觸發..要更改 – 2012-01-30 14:49:59
那麼我確實改變了它...將選擇從0移動到1不會被認爲是改變了嗎? – 2012-01-30 14:51:30