我目前正在为我的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