Spinner.setSelection不能正确触发OnItemSelectedListener。

18
我现在正在为我的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绑定监听器的相关行:
    /** 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()调用,但这并没有改变应用程序的反应。
谢谢提前提供任何提示,帮助或技巧!

当您在下拉列表中执行某些操作以更改选项时,才会触发OnItemSelectedListener。 - Senthil Mg
1
我确实改了它...把选择从0移动到1,这不算改变吗? - Jean-Philippe Roy
3个回答

27

你尝试过使用两个参数来设置Spinner,第二个参数使用布尔值吗:

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

开发者页面上显示:

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

我已经尝试使用animate参数为false(从另一个线程中获取),现在我将尝试将参数设置为true。 - Jean-Philippe Roy
3
非常感谢,我本应该一开始就尝试将其设置为true :P。将两个微调框的setSelection(pos, true)都设置为true就解决了问题。 - Jean-Philippe Roy
2
我刚刚测试了一下,使用带有animate方法的方法确实会触发onItemSelected,而没有带有该方法的则不会。 - Marcel Bro
5
为什么Android这么烂! - menawi
4
我也无法使用:( @AmanGoel的答案也对我无效。 - or_dvir
我已经浪费了2个小时寻找这个答案。怎么可能这么难做这么简单的事情? - jorgeavilae

5
只需要使用以下代码:
  ownerSpinnerVw.post(new Runnable() {
        @Override
        public void run() {
             ownerSpinnerVw.setSelection(position);
        }
    });

0
我发现解决我的问题是将这个添加到onCreate方法中。程序可以工作,但仅适用于第一次选择。第二次选择时,程序会导致模拟器崩溃。
spinner.setOnItemSelectedListener(this);

enter image description here


正如您在帖子中提到的那样,这个答案并不起作用。请将其添加为评论。 - Arashsoft

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接