【发布时间】:2017-12-18 14:11:50
【问题描述】:
我目前正在为我的 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 方法中,但这并没有改变应用程序的反应方式。
提前感谢任何指示、帮助或提示!
【问题讨论】:
-
,OnItemSelectedListener 仅在您对微调器执行某些操作时才会触发..更改
-
嗯,我确实改变了它......将选择从 0 移动到 1 不会被视为改变吗?
标签: android spinner android-spinner