Android:如何将微调器绑定到自定义对象列表?
在用户界面中必须有一个微调器,其中包含一些名称(名称是可见的),并且每个名称都有自己的 ID(ID 不等于显示顺序)。当用户从列表中选择名称时,必须更改变量 currentID。
该应用程序包含 ArrayList
,其中 User 是一个具有 ID 和名称的对象:
public class User{
public int ID;
public String name;
}
我不知道的是如何创建一个显示用户姓名列表的微调器并将微调器项目绑定到 ID,以便在选择/更改微调器项目时变量 currentID 设置为适当的值。
如果有人可以展示所描述问题的解决方案或提供任何有助于解决问题的链接,我将不胜感激。
谢谢!
In the user interface there has to be a spinner which contains some names (the names are visible) and each name has its own ID (the IDs are not equal to display sequence). When the user selects the name from the list the variable currentID has to be changed.
The application contains the ArrayList
Where User is an object with ID and name:
public class User{
public int ID;
public String name;
}
What I don't know is how to create a spinner which displays the list of user's names and bind spinner items to IDs so when the spinner item is selected/changed the variable currentID is set to appropriate value.
I would appreciate if anyone could show the solution of the described problem or provide any link useful to solve the problem.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
对于简单的解决方案,您可以覆盖对象中的“toString”
,然后您可以使用:
这样您的微调器将仅显示用户名。
For simple solutions you can just Overwrite the "toString" in your object
and then you can use:
This way your spinner will show only the user names.
您可以查看此答案。您也可以使用自定义适配器,但下面的解决方案适用于简单的情况。
这里是一个转发:
因此,如果您来到这里是因为您想在 Spinner 中同时拥有标签和值 - 这就是我的做法:
Spinner
array.xml
文件 - 一个标签数组,一个值数组android:entries="@array/labels"
Spinner >当您需要一个值时,请执行以下操作(不,您不必链接它):
You can look at this answer. You can also go with a custom adapter, but the solution below is fine for simple cases.
Here's a re-post:
So if you came here because you want to have both labels and values in the Spinner - here's how I did it:
Spinner
the usual wayarray.xml
file -- one array for labels, one array for valuesSpinner
withandroid:entries="@array/labels"
When you need a value, do something like this (no, you don't have to chain it):
到目前为止,我发现的最简单的方法:
现在您可以将任何对象粘贴到微调器中,它将显示指定的标签。
By far the simplest way that I've found:
Now you can stick any object in your spinner, and it will display the specified label.
只需对 Joaquin Alberto 的答案做一点小小的调整就可以解决样式问题。只需替换自定义适配器中的 getDropDownView 函数,如下所示,
Just a small tweak to Joaquin Alberto's answer can solve the style issue.Just replace the getDropDownView function in the custom adapter as below,
受到华金·阿尔贝托 (Joaquin Alberto) 的启发,这对我有用:
inspired by Joaquin Alberto, this worked for me:
对我来说效果很好,围绕 getResource() 所需的代码如下:
只需确保(自己)两个数组中的值正确对齐!
Works fine for me, the code needed around the getResource() thing is as follows:
Just need to make sure (by yourself) the values in the two arrays are aligned properly!
基于 Joaquin Alberto (感谢)示例,但它适用于任何类型(您应该在类型中实现 toString() ,这样您就可以格式化输出。
另外我认为您可以用数组替换 List,这样您就不需要执行 toArray在列表中,但我有一个列表......:)
Based on Joaquin Alberto (thanks) sample, but it works for any type (you should implement toString() in type, so you can format the output.
Also i think you can replace List by Array so you don't need to do toArray in List, but i had a List ..... :)
kotlin:
实现
kotlin:
Implementation
为了理解这一技巧,我们必须了解适配器的一般工作原理,特别是 ArrayAdapter 的工作原理。
适配器:是能够将数据结构绑定到小部件的对象,然后这些小部件在列表或微调器中显示该数据。
因此,适配器回答的两个问题是:
EditText
?ArrayAdapter 的答案是:
后裔)。小部件的
.setText()
方法将与支持数据结构中项目的字符串格式。这
字符串格式将通过在项目上调用
.toString()
来获取。CustomListViewDemo.java
adaptor_content.xml
main.xml
它工作正常,我希望它有用。
In order to understand the trick, one has to know, how Adapters work in general and ArrayAdapter in particular.
Adapters: are objects that are able to bind data structures to widgets, then these widgets are displaying that data in a List or in a Spinner.
So the two questions an Adapter answers are:
EditText
of the widget or composite view according to this data?ArrayAdapter's answers are:
row.xml
ORandroid.R.layout.simple_spinner_item
) for any index is the same, and is inflated from the resource whose ID was given to ArrayAdapter's constructor.descendant). The widget's
.setText()
method will be used with thestring format of the item in the supporting data structure. The
string format will be obtained by invoking
.toString()
on the item.CustomListViewDemo.java
adaptor_content.xml
main.xml
It works properly, I hope it is useful.
这是 Kotlin 中的完整过程:
微调器适配器类:
并在您的活动/片段中使用它,如下所示:
Here's the complete process in Kotlin:
the spinner adapter class:
and use it in your activity/fragment like this:
我的自定义对象是
我还想将此对象设置为我的微调器的适配器源,而不扩展 ArrayAdapter 所以我所做的是。
现在您将能够在微调器中看到结果,技巧是在您的自定义对象中重写
toString()
,因此您想要在微调器中显示的值只需在这个方法中返回它。My custom Object is
I also wanted to set this object as my adapter source to my spinner without extending ArrayAdapter so that what I did was.
Now You will be able to see results in your spinner, the trick was to override
toString()
in you custom object, so what ever value you want to display in spinner just return that in this method.如果您不需要单独的类,我的意思是只需映射到您的对象上的简单适配器。
这是我基于提供的 ArrayAdapter 函数的代码。
因为您可能需要在创建适配器后添加项目(例如数据库项目异步加载)。
简单但高效。
然后您可以使用此代码(我无法将 Category[] 放入适配器构造函数中,因为数据是单独加载的)。
请注意,adapter.addAll(items)通过在内部调用notifyDataSetChanged()来刷新微调器。
If you don't need a separated class, i mean just a simple adapter mapped on your object.
Here is my code based on ArrayAdapter functions provided.
And because you might need to add item after adapter creation (eg database item asynchronous loading).
Simple but efficient.
And then you can use this code (i couldn't put Category[] in adapter constructor because data are loaded separatly).
Note that adapter.addAll(items) refresh spinner by calling notifyDataSetChanged() in internal.
我认为最好的解决方案是 “最简单的解决方案” by 乔什·品特。
这对我有用:
I think that the best solution is the "Simplest Solution" by Josh Pinter.
This worked for me:
做:
Do:
我知道线程很旧,但以防万一...
用户对象:
自定义微调适配器(ArrayAdapter)
和实现:
I know the thread is old, but just in case...
User object:
Custom Spinner Adapter (ArrayAdapter)
And the implementarion:
最简单的解决方案
在搜索了不同的解决方案后,我发现以下是使用自定义
对象
填充Spinner
的最简单、最干净的解决方案。这是完整的实现:User.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:User.java
res/layout/spinner.xml
res/layout/your_activity_view.xml
In Your Activity