Android:带有不断变化的 ArrayAdapter 的 Spinner - 如何识别?
我想要三个 Spinner,其内容相互依赖。
例如,Spinner1 显示 {item1, item2},Spinner2 显示 {item3, item4} 或 {item5, item6},具体取决于在 Spinner1 上选择了 item1 还是 item2。 我想要的 Spinner 3 也是如此,它对 Spinner1 和/或 Spinner2 的变化做出反应。
对于后者,我必须首先确定 Spinner2 中显示了哪些可能的值集。
我的问题有点类似于 这个问题,但是我不知道拿到适配器后该怎么办。
这就是我到目前为止所拥有的:
ArrayAdapter adapter1 = (ArrayAdapter) spinner2.getAdapter();
if(items_spinner1[0].contentEquals(adapter1.getItem(0)))
{
//...
}
我获取适配器,请求第一个值并将其与数组的第一个字符串值进行比较以识别它。对我来说,它一点也不优雅。有更简单的解决方案吗?
I want to have three Spinners with contents which depend on each other.
E.g. Spinner1 displays {item1, item2} and Spinner2 either {item3, item4} or {item5, item6} depending on whether item1 or item2 is selected on Spinner1.
The same I want for Spinner 3, which reacts to changes of Spinner1 and/or Spinner2.
For the latter, I have to determine first which of the possible value sets is shown atm in Spinner2.
My question is kind of similar to this question, but I don't know what to do after getting the adapter.
That's what I have so far:
ArrayAdapter adapter1 = (ArrayAdapter) spinner2.getAdapter();
if(items_spinner1[0].contentEquals(adapter1.getItem(0)))
{
//...
}
I get the adapter, ask for the first value and compare it to the first String value of my Array to identify it. It doesn't at all seem elegant to me. Is there an easier solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您说后面的旋转器的内容取决于前面旋转器中的选择,但您发布的代码仅取决于旋转器的内容。
adapter1.getItem(0)
返回列表中的第一个项目,而不是当前选定的项目。要获取当前选定的项目,请使用微调器(而不是适配器)的getSelectedItem()
方法。例如,您可以在第一个 Spinner 的 onItemSelectedListener 中放置类似的内容(根据下面的评论进行编辑):
然后在第二个 Spinner 的 onItemSelectedListener 中放置类似的内容。使用
getSelectedItem()
从第一个和第二个 Spinner 中获取选定的项目(或者使用getSelectedItemId()
对于第一个 Spinner 获取项目位置,对于第二个 Spinner 使用位置参数)。使用选定的项目设置第三个。编辑:第二个 Spinner 的 OnItemSelectedListener 看起来像这样。
You say the contents of the later spinners depend on the selection in the earlier ones, but the code you posted depends only on the contents of a spinner.
adapter1.getItem(0)
returns the first item in the list, not the currently selected item. To get the currently selected item, use the spinner's (not the adapter's)getSelectedItem()
method.You could, for example, put something like this in your first Spinner's onItemSelectedListener (edited based on your comment below):
Then place something similar in the second Spinner's onItemSelectedListener. Get the selected items from the first and second Spinners using
getSelectedItem()
(or the item positions usinggetSelectedItemId()
for the first and the position parameter for the second). Use the selected items to set up the third.Edit: The OnItemSelectedListener for the second Spinner would look something like this.