安卓Spinner.setSelection()不工作 [英] Android Spinner.setSelection() doesn't work
问题描述
我在其中填充了一个的CursorAdapter
A微调。现在创建微调(动态)我想设置一个默认选择不同0(0是在的CursorAdapter
列表中的第一项)
I'm having a spinner which is populated with a CursorAdapter
. Now when creating that spinner (dynamically) I want to set a default selection different to 0 (0 being the first item in the CursorAdapter
list)
所以,我得到了一个setter方法,这只是调用的对象被创建后的工作。但由于某些原因,不管是什么我通过在二传,0总是被传递给在 onItemSelected()
方法Spinner的 OnItemSelectedListener
。
So I got a setter for that job that is just called after the object has been created. But for some reason, no matter what I pass in that setter, 0 always is passed to the onItemSelected()
method within the Spinner's OnItemSelectedListener
.
但是,如果我只是一直等到最初的最初的选择发生了,再次运行我的二传手,一切工作正常。因此,为了更清楚这里是我在调试器中看到:
HOWEVER if I just wait till the first initial selection happened and run my setter again, everything works fine. So, to make it more clear here is what I see in the debugger:
- 对象(微调)创建
- 微调侦听器附加
- 在二传手运行时,一个数字,可以说4(微调方式包含超过4个选项)传递给
setSelection()
方法 - 等一会
- 现在,
onItemSelected()
运行的第一次,但考虑到该方法的位置为0! - 在等待多一点
- 我们再次运行二传手
- 再次
onItemSelected()
运行,这个时候,给出的排名位置是4,因为它应该是!
- object (spinner) is created
- spinner listener is attached
- setter is run, a number, lets say 4 (the spinner contains way more than 4 selections) is passed to the
setSelection()
method - WAIT A BIT
- NOW
onItemSelected()
is run the first time, but the position given to that method is 0! - WAIT A BIT MORE
- again we run the setter
- again
onItemSelected()
is run, THIS TIME, the position given is 4 as it was supposed to be!
现在这是有线。这似乎并不可能,以设置微调,直到第一个最初的选择,这始终是0,已运行。那么,为什么事情是这样的,我能做些什么来设置初始选择?
Now that's wired. It doesn't seem to be possible to set that spinner till the first initial selection, which is always 0, was run. So why is it the way it is and what can I do to set the initial selection?
推荐答案
尝试微调#setSelection(INT位置,布尔动画)
用动画= FALSE。我记得前阵子我也有类似的问题,这并获得成功。内部实现看起来从差从动画部分分崩离析不同。
Try Spinner#setSelection (int position, boolean animate)
with animate = false. I remember a while back I had a similar problem and this did the trick. The internal implementation seems to differ apart from the difference coming from the animate part.
这篇关于安卓Spinner.setSelection()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!