[英]Spinner.setSelection doesn't trigger OnItemSelectedListener properly
我正在為我的Android應用程序開發一個帳戶管理活動,但是我很難弄清楚為什么來自微調器的setSelection()方法不會觸發附加到所述Spinner的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()方法的相關行,該方法在Activity的創建中調用,該方法顯示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
這是我的LogCat提取,顯示當選擇應用於country spinner時,不會調用Listener:
I / ManageAccountActivity(28108):根據用戶信息設置國家/地區。
I / ManageAccountActivity(28108):根據用戶信息設置省份。
I / ManageAccountActivity(28108):調用onCountrySelected(),位置:1
作為一個實驗,我也嘗試將fillUI()調用放在我的Activity的onStart方法中,但這並沒有改變應用程序的反應方式。
在此先感謝您的任何指示,幫助或提示!
您是否嘗試使用兩個參數設置微調器,第二個使用布爾值:
.setSelection(mProvincesCode.indexOf(mUser.getProvince()), true);
從開發者頁面顯示:
setSelection(int position, boolean animate)
//Jump directly to a specific item in the adapter data.
只需使用以下代碼:
ownerSpinnerVw.post(new Runnable() {
@Override
public void run() {
ownerSpinnerVw.setSelection(position);
}
});
我發現如果你聲明,setSelection(pos)是有效的
yourSpinner.setOnItemSelectedListener(null);
在那之前。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.