Spinner.setSelection不会正确触发OnItemSelectedListener

时间:2012-01-30 14:43:24

标签: android spinner android-spinner

我目前正在为我的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摘录,显示当选择应用于国家/地区微调器时,不会调用监听器:

  

I / ManageAccountActivity(28108):根据用户信息设置国家/地区。

     

I / ManageAccountActivity(28108):根据用户信息设置省份。

     

I / ManageAccountActivity(28108):onCountrySelected()被调用,位置:1

作为一个实验,我也尝试将fillUI()调用放在我的Activity的onStart方法中,但这并没有改变应用程序的反应方式。

提前感谢任何指示,帮助或提示!

4 个答案:

答案 0 :(得分:23)

您是否尝试使用两个参数设置微调器,第二个使用布尔值:

.setSelection(mProvincesCode.indexOf(mUser.getProvince()), true); 

从它显示的developers page

setSelection(int position, boolean animate)
//Jump directly to a specific item in the adapter data.

答案 1 :(得分:3)

只需使用以下代码:

  ownerSpinnerVw.post(new Runnable() {
        @Override
        public void run() {
             ownerSpinnerVw.setSelection(position);
        }
    });

答案 2 :(得分:0)

我发现我的问题的解决方案是将其添加到onCreate方法中。该程序有效但仅适用于第一次选择。第二次选择程序会使模拟器崩溃。

spinner.setOnItemSelectedListener(this);

enter image description here

答案 3 :(得分:0)

I found that setSelection(pos) works if you declare

yourSpinner.setOnItemSelectedListener(null);

before that.