屏幕旋转后多次调用Android微调器onItemSelected [英] Android spinner onItemSelected called multiple times after screen rotation
问题描述
我有一个由三个微调器组成的布局.它们在下拉菜单中提供的选项有所不同.
在我的onCreateView
中,我有一种设置微调器的方法.在该方法中,我有类似以下内容:
I have a layout with three spinners. They differ in the option presented in the drop-down.
In my onCreateView
I have a method to setup the spinners. Inside that method I have something like this:
mySpinner = (Spinner) view.findViewById(R.id.my_spinner);
ArrayAdapter<String> mySpinner =
new ArrayAdapter<String>(getActivity(), R.layout.background,
new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.spinner_one_data))));
mySpinner.setDropDownViewResource(R.layout.spinner_text);
mySpinner.setAdapter(mySpinner);
mySpinner.setOnItemSelectedListener(this);
正如我所说,我的其他两个微调器几乎相同,但是选择不同.
As I said, my other two spinners are almost the same but with different options.
我知道onItemSelected
在首次设置"中为每个微调器调用一次,因此我有一个标志来防止此问题.使用此标志解决方案,我的微调器正在按预期工作.
I know that onItemSelected
is called once for every spinner in a "first setup" so I have a flag to prevent this problem. With this flag solution, my spinners are working as expected.
问题是当我在每个微调器中选择一个选项然后旋转屏幕时.现在,onItemSelected
被调用了6次,而不是我期望的3次(我已经设置了一个标志来管理3次调用的这种情况).
The problem is when I select in each spinner an option and then rotate the screen. Now, onItemSelected
is called 6 times instead the 3 times that I was expecting (I've set a flag to manage this situation of the 3 times calling).
为什么会发生这种情况,头该怎么办?
Why Is it happening and hoe should I handle this?
推荐答案
我找到了适合我的解决方案.
I've found a solution that is working for me.
我有3
个微调器,因此在初始微调器设置中onItemSelected
被称为3
次.为了避免onItemSelected
在初始设置中触发方法,我创建了一个计数器,因此onItemSelected
仅相应地触发该方法的计数器值.
I have the 3
spinners so onItemSelected
is called 3
times at the initial spinner setup. To avoid onItemSelected
from firing a method in the initial setup I've created a counter so onItemSelected
only fires the method accordingly the counter value.
我已经意识到,在我的情况下,如果旋转屏幕,会再次触发onItemSelected
次3
次,再加上每个不在0
位置的微调器的时间.
I've realized that in my situation, if a rotated the screen, onItemSelected
is fired again the 3
times, plus a time for each spinner that is not in the position 0
.
一个例子:
我有3
个微调框,用户将其中的2
更改为一个可用选项中的一个,然后将其定位为0
,因此他最终遇到了这样的情况:
I have the 3
spinners and the user changes 2
of them to one of the available option other then position 0
so he ends up with a situation like this:
First spinner - > Item 2 selected
Second spinner -> Item 0 selected (no changes)
Third spinner -> Item 1 selected
现在,我旋转屏幕,对于初始微调器设置,将触发onItemSelected
次3
次,对于不在位置0
的微调器,将触发2
次.
Now, wen I rotate the screen, onItemSelected
will be fired 3
times for the initial spinner setup plus 2
times for the spinners that aren't at position 0
.
@Override
public void onSaveInstanceState(Bundle outState) {
int changedSpinners = 0;
if (spinner1.getSelectedItemPosition() != 0) {
changedSpinners += 1;
}
if (spinner2.getSelectedItemPosition() != 0) {
changedSpinners += 1;
}
if (spinner3.getSelectedItemPosition() != 0) {
changedSpinners += 1;
}
outState.putInt("changedSpinners", changedSpinners);
}
我已将状态保存在onSaveInstanceState
中,然后在onCreateView
中检查了savedInstanceState != null
是否存在,是否从包中提取了changedSpinners
并更新了计数器以进行相应操作.
I've saved the state in onSaveInstanceState
and then, in onCreateView
I checked if savedInstanceState != null
and if so, extracted changedSpinners
from the bundle and updated my counter to act accordingly.
这篇关于屏幕旋转后多次调用Android微调器onItemSelected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!