使用自定义对象填充微调器 [英] Populate spinner with custom object
问题描述
我想用 SQLite 表中的数据填充我的 Android 微调器.我已经有了数据库控制器,它们正在返回自定义对象.
I want to populate my Android spinner with data from a SQLite table. I already have the database controllers, and these are returning custom objects.
现在我需要做的是用这些对象填充微调器,以便得到的不是字符串字段,而是 Id 字段.例如:我的表 Days,包含字段 Id 和 Day:
Now what I need to do is populate the spinner with this objects, in order to get not the string field, but the Id field. For example: My table Days, with fields Id and Day:
id - 天1 - 星期日
id - day 1 - Sunday
2 - 星期一
3 - 周二...
Day 对象有两个属性(id 和 name)
And the Day object has two properties (id, and name)
我希望微调器显示星期一",但在内部,将 Id 字段2"存储在一个 int 变量中,以便使用这些选定值再次查询数据库.
I want the spinner to show "Monday", but internally, store the Id field '2' in an int variable, in order to query again the database with theese selected values.
我想我必须用适配器来做,但我不知道我是否可以用这个控制器做我想做的事.
I think I have to do it with an Adapter, but I don't know if I can do what I want with this controller.
我希望能得到一些命令或建议.
I'd apreciate some order or advice to begin with.
谢谢
推荐答案
您可以使用 ArrayAdapter
通过方法 setAdapter
来填充微调器.
You can use ArrayAdapter<T>
to populate your spinner via the method setAdapter
.
private List<MyData> data;
...
mySpinner.setAdapter<MyData>(new ArrayAdapter(getContext(), resId, data));
...
public class MyData {
private int _id;
public MyData(int id) {
this._id = id;
}
@Override
public String toString() {
switch (this._id) {
....
}
}
}
这篇关于使用自定义对象填充微调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!