我现在正在为我的Android应用程序开发一个帐户管理活动,并且我遇到了问题,不知道为什么从下拉列表中setSelection() 方法不会触发附加到该下拉列表的OnItemSelectedListener。
以下是我目前拥有的内容;
onCreate()方法:
在Activity创建时调用的initializeUI()方法中,显示Spinner绑定监听器的相关行:
再来几行,这次是来自 fillUI 方法的代码:
因此,我期望在fillUI()方法中设置选择后立即调用OnItemSelectedListener,但是在运行时并没有发生这种情况:S。
以下是LogCat提取,显示当选择应用于国家微调器时未调用侦听器:
I / ManageAccountActivity(28108):根据用户信息设置国家。 I / ManageAccountActivity(28108):根据用户信息设置省份。 I / ManageAccountActivity(28108):onCountrySelected()被调用,位置:1
作为一项实验,我还尝试在我的Activity的onStart方法中放置fillUI()调用,但这并没有改变应用程序的反应。
谢谢提前提供任何提示,帮助或技巧!
以下是我目前拥有的内容;
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绑定监听器的相关行:
/** 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。
以下是LogCat提取,显示当选择应用于国家微调器时未调用侦听器:
I / ManageAccountActivity(28108):根据用户信息设置国家。 I / ManageAccountActivity(28108):根据用户信息设置省份。 I / ManageAccountActivity(28108):onCountrySelected()被调用,位置:1
作为一项实验,我还尝试在我的Activity的onStart方法中放置fillUI()调用,但这并没有改变应用程序的反应。
谢谢提前提供任何提示,帮助或技巧!