在没有哈希图结构的微调器中使用键和值
Using key and value in spinner without hashmap structure
我有键和值列表,
key = 是来自 table 的 id 字符串。
value = 是名称的字符串,
我只想在微调器视图中显示不带 ID 的值,但在用户点击某些项目后,我需要获取所选项目的 ID,并在代码中使用它。
如何在不使用 hashMap 或许多 foreach 语句的情况下实现此结构?
谢谢,
点击侦听器接收点击视图。
Class 视图支持 'ViewHolder' 模式(如在 RecyclerView 中),其中对所有小部件的引用存储在 ViewHolder 中,以后可以访问。您可以使用相同的方法 - 创建 class,包含一些信息和对点击侦听器的引用,然后通过 View.setTag() 在创建时将其设置为您的下拉项。选择项目时,查询此对象并调用您要执行的可运行程序-您的操作
创建一个 POJO class 并将自定义对象的 ArrayList
传递给微调器。
class MyClass{
private String id;
private String key;
public MyClass(String id, String value) {
this.id = id;
this.value = value;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}
重写 toString()
方法很重要。这就是将在微调项中显示的内容。
您可以像这样将它传递给您的 ArrayAdapter
:
ArrayAdapter userAdapter = new ArrayAdapter(this, R.layout.spinner, myList);
当用户点击一个项目时,getSelectedItem()
方法会给你对象。你可以从那里得到 id
。
来自docs:
A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.
However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.
我有键和值列表, key = 是来自 table 的 id 字符串。 value = 是名称的字符串,
我只想在微调器视图中显示不带 ID 的值,但在用户点击某些项目后,我需要获取所选项目的 ID,并在代码中使用它。 如何在不使用 hashMap 或许多 foreach 语句的情况下实现此结构?
谢谢,
点击侦听器接收点击视图。 Class 视图支持 'ViewHolder' 模式(如在 RecyclerView 中),其中对所有小部件的引用存储在 ViewHolder 中,以后可以访问。您可以使用相同的方法 - 创建 class,包含一些信息和对点击侦听器的引用,然后通过 View.setTag() 在创建时将其设置为您的下拉项。选择项目时,查询此对象并调用您要执行的可运行程序-您的操作
创建一个 POJO class 并将自定义对象的 ArrayList
传递给微调器。
class MyClass{
private String id;
private String key;
public MyClass(String id, String value) {
this.id = id;
this.value = value;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}
重写 toString()
方法很重要。这就是将在微调项中显示的内容。
您可以像这样将它传递给您的 ArrayAdapter
:
ArrayAdapter userAdapter = new ArrayAdapter(this, R.layout.spinner, myList);
当用户点击一个项目时,getSelectedItem()
方法会给你对象。你可以从那里得到 id
。
来自docs:
A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.
However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.