使用数组列表的 Android Spinner 数据绑定
我有一个像这样的数组列表:
private ArrayList<Locations> Artist_Result = new ArrayList<Location>();
这个Location类有两个属性:id
和location
。
我需要将我的 ArrayList 绑定到微调器。我已经尝试过这种方式:
Spinner s = (Spinner) findViewById(R.id.SpinnerSpcial);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, Artist_Result);
s.setAdapter(adapter);
但是,它显示了对象的十六进制值。所以我想我必须设置显示该微调器控制器的文本和值。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ArrayAdapter
尝试通过调用 Location 对象显示为字符串(这会导致十六进制值)。 com/javase/6/docs/api/java/lang/Object.html#toString%28%29">Object.toString()
-方法。它的默认实现返回:要使
ArrayAdadpter
显示项目列表中实际有用的内容,您可以重写toString()
-返回有意义的内容的方法:另一种方法是,扩展 BaseAdapter 和实现 SpinnerAdapter 来创建您自己的适配器,它知道您的
ArrayList< 中的元素/code> 是对象以及如何使用这些对象的属性。
[修订] 实现示例
我玩了一下,我设法让一些东西起作用:
我对代码进行了充分的注释,如果您有任何问题,请随时询问他们。
The
ArrayAdapter
tries to display yourLocation
-objects as strings (which causes the Hex-values), by calling theObject.toString()
-method. It's default implementation returns:To make the
ArrayAdadpter
show something actually useful in the item list, you can override thetoString()
-method to return something meaningful:Another way to do this is, to extend BaseAdapter and implement SpinnerAdapter to create your own Adapter, which knows that the elements in your
ArrayList
are objects and how to use the properties of those objects.[Revised] Implementation Example
I was playing around a bit and I managed to get something to work:
I fully commented the code, if you have any questions, don't hesitate to ask them.
最简单的解决方案
在搜索了不同的解决方案后,我发现以下是使用自定义
对象
填充Spinner
的最简单、最干净的解决方案。这是完整的实现:Location.java
res/layout/spinner.xml
res/layout/your_activity_view.xml
在您的活动中
Simplest Solution
After scouring different solutions on SO, I found the following to be the simplest and cleanest solution for populating a
Spinner
with customObjects
. Here's the full implementation:Location.java
res/layout/spinner.xml
res/layout/your_activity_view.xml
In Your Activity
感谢卢卡斯上面(下面?)的回答,我能够开始解决这个问题,但我的问题是他的
getDropDownView
实现使下拉项只是一个纯文本 - 所以没有填充,也没有漂亮的绿色单选按钮,就像使用android.R.layout.simple_spinner_dropdown_item
时看到的那样。如上所述,除了
getDropDownView
方法为:Thanks to Lukas' answer above (below?) I was able to get started on this, but my problem was that his implementation of the
getDropDownView
made the dropdown items just a plain text - so no padding and no nice green radio button like you get when using theandroid.R.layout.simple_spinner_dropdown_item
.So as above, except the
getDropDownView
method would be:好吧,我不会与更多细节混淆。
只需创建您的
ArrayList
并像这样绑定您的值。假设您的布局上已经有一个微调控件,其 id 为
spinner1
。在下面添加此代码。上面的所有代码都在您的
onCreate
函数下。Well, am not gonna confuse with more details.
Just create your
ArrayList
and bind your values like this.Assuming that you have already a spinner control on your layout say with id,
spinner1
. Add this code below.All the above code goes under your
onCreate
function.谢谢卢卡斯,你帮了我很多忙。
我想改进你的答案。
如果您只想稍后访问所选项目,可以使用这个:
因此您不必在初始化中使用 setOnItemSelectedListener() :)
Thank Lukas, you help me a lot.
I d'like to improve your answer.
If you just want to access the selected item later, you can use this :
So you don't have to use the setOnItemSelectedListener() in your initialisation :)