我目前正在为我的 Android 应用程序开发帐户管理活动,但我无法弄清楚为什么微调器的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();
}
}
在 Activity 创建时调用的 initializeUI() 方法的相关行显示了 Spinner 和 Listener 的绑定:
/** 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()));
所以有了这个,我希望在我在 fillUI() 方法中设置选择之后立即调用 OnItemSelectedListener,但这不是运行时发生的事情:S
Here's my LogCat extract that shows that the Listener isn't called when the selection is applied to the country spinner:
I/ManageAccountActivity(28108):根据用户信息设置国家。
I/ManageAccountActivity(28108):根据用户信息设置省份。
I/ManageAccountActivity(28108):onCountrySelected() 被调用,位置:1
作为一个实验,我还尝试将 fillUI() 调用放在我的 Activity 的 onStart 方法中,但这并没有改变应用程序的反应方式。
在此先感谢您的任何指示、帮助或提示!