Android 生成没有 XML 或 R.array 的 Spinner 列表
我需要动态生成微调器弹出窗口中的值。
具体来说,我显示了一个年份列表供用户选择。年份是当前年份+未来12年。
如果我使用 strings.xml 来创建字符串数组,那么我已经在该数组中手动输入了列表项。几年后,它仍然会显示出更古老的无关年份。我必须推送更新或其他东西。瘸。
我知道如何使用整数形式获取当前年份,
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
但我需要用今年+未来几年填充一个数组。这很容易做到,但 Spinner 的适配器类型都不想采用 int 数组或字符串数组。
我需要一个显示动态生成的项目的微调器。 strings.xml 包含预先输入的列表项,因此我无法使用它。
帮助?
I need the values within a spinner popup to be dynamically generated.
Specifically I am showing a list of years for a user to choose from. The year is the current year + 12 years in the future.
If I used strings.xml to make a string-array, then I have manually typed in list-items in that array. A few years from now, it will still be showing older irrelevant years. I would have to push an update or something. Lame.
I know how to get the current year in Integer form using
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
but I need to populate an array with this year + future years. This is easy to do, but none of the adapter types for Spinner want to take int-arrays, or string-arrays for that matter.
I need a spinner that display items that were dynamically generated. The strings.xml contains pre-typed list-items so I cannot use that.
Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过将它们作为整数的字符串化版本一次添加一个?
我还没有尝试编译这个,但是这样的东西应该可以工作:
然后,如果您需要它们作为 int 退出,请使用
Integer.parseInt()
Have you tried adding them in as String-ified versions of ints one at a time?
I haven't tried compiling this, but something like this should work:
And then if you need them back as int's on the way out, use
Integer.parseInt()
您可以扩展 SpinnerAdapter(或 BaseAdapter 或 ArrayAdapter)类并使用它们执行您需要的任何操作。
You can extend the SpinnerAdapter (or BaseAdapter or ArrayAdapter) class and do whatever you need with them.