Android 微调器选择同一项目两次

Android 微调器选择同一项目两次,android,spinner,onitemselectedlistener,Android,Spinner,Onitemselectedlistener,我创建了一个由国旗和每个国旗的国家代码填充的微调器: 但我面临一个问题。当我删除editText上的国家代码编号时,我希望能够返回微调器,在国旗上再单击一次,并显示我刚刚删除的正确国家代码 但目前我无法再次触发事件 用例: 例如,在这里,对于英国国旗和代码,我通过微调器选择英国国旗,触发我的事件OnItemSelectedListner,并将+44国家代码设置到我的编辑文本中 然后我错误地删除了它,所以我要返回到我的微调器我在我的英国国旗上单击了一个新的时间,但什么都没有发生,我的事件On

我创建了一个由国旗和每个国旗的国家代码填充的微调器:

但我面临一个问题。当我删除editText上的国家代码编号时,我希望能够返回微调器,在国旗上再单击一次,并显示我刚刚删除的正确国家代码

但目前我无法再次触发事件

  • 用例:
例如,在这里,对于英国国旗和代码,我通过微调器选择英国国旗,触发我的事件OnItemSelectedListner,并将+44国家代码设置到我的编辑文本中

然后我错误地删除了它,所以我要返回到我的微调器我在我的英国国旗上单击了一个新的时间,但什么都没有发生,我的事件OnItemSelectedListner没有被触发,而我单击了我的国旗

在这一点上,如果我点击列表中的另一个单位,它的代码将显示,我可以返回英国国旗点击它显示代码,这一次它工作

我正在试图找出为什么无法触发此事件:再次触发OnItemSelectedListner。好像我的项目已经被选中,此时不可能发生任何事件

我的代码:

new AsyncPhoneInitTask(currentActivity.this).execute();// fetch data to fill spinner
countrySpinner = (Spinner) findViewById(R.id.spinner);
countrySpinner.setOnItemSelectedListener(onItemSelectedListener);
countryAdapter = new CountryAdapter(currentActivity.this);
countrySpinner.setAdapter(countryAdapter);

// I want to trigger this event a second time
protected AdapterView.OnItemSelectedListener onItemSelectedListener = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            Country c = (Country) countrySpinner.getItemAtPosition(position);

            // My code
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    };
新建AsyncPhoneInitTask(currentActivity.this).execute();//获取数据以填充微调器
countrySpinner=(Spinner)findViewById(R.id.Spinner);
countrySpinner.setOnItemSelectedListener(onItemSelectedListener);
countryAdapter=新的countryAdapter(currentActivity.this);
countrySpinner.setAdapter(countryAdapter);
//我想再次触发这个事件
受保护的AdapterView.OnItemSelectedListener OnItemSelectedListener=新AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
Country c=(国家)countrySpinner.getItemAtPosition(位置);
//我的代码
}
@凌驾
未选择公共无效(AdapterView父级){
}
};
有办法解决我的问题吗


我希望有一个事件SetonimClickListener,而不是SetonimSelectedListener。因为这就是为什么我不能两次重新选择同一个项目的原因,它已经被选中了。

我发现的一个解决办法是在微调器中有一个“占位符”,就像列表中的第一个项目是“选择国家”。然后加上

countrySpinner.setSelection(0);
onItemSelected()
。这将强制微调器交换所选的项目,从而导致
onItemSelected()
发生两次,一次是用户预期的选择,一次是“选择国家/地区”选项。由于此解决方法还会再次触发onItemSelected,因此需要在开始时进行检查以避免内循环

int positionOfPlaceholder = 0;

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    //Check if the first element is choosen
    if(i == positionOfPlaceholder){
        return;
    }
    Country c = (Country) countrySpinner.getItemAtPosition(position);

    //Code

    countrySpinner.setSelection(positionOfPlaceholder);
}
intpositionofplaceholder=0;
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
//检查是否选择了第一个元素
if(i==定位器的位置){
返回;
}
Country c=(国家)countrySpinner.getItemAtPosition(位置);
//代码
countrySpinner.setSelection(定位器的位置);
}

这里的问题是,choosen值从未显示在微调器中,因为您似乎也希望…

您可以显示用于重新打开或重新初始化微调器的代码吗?如果第二次旋转器没有连接onItemSelectedListener,那么这可能是问题的根源。@FrankD。我没有任何代码来重新初始化微调器。我只想有一个事件setOnItemClickListener,而不是setOnItemSelectedListener。因为这里是我无法重新选择相同项目的原因,它已经被选中。