我想将zone_id设置为spinner中的item以及来自json的区域名称
[{"zone_id":1,"zone_name":"North"},{"zone_id":2,"zone_name":"West"},{"zone_id":3,"zone_name":"South"},{"zone_id":4,"zone_name":"East"}]
我创建了一个自定义类
public class StringWithTag{
public Object itemId;
public String itemName;
public StringWithTag(Object itemId,String itemName){
this.itemId = itemId;
this.itemName = itemName;
}
}
将项目添加到列表中
instituteArrayList.add(new StringWithTag(itemId,itemName));
但问题是在下拉列表中我得到了对象的引用
我希望区域显示在微调器中,当我点击任何区域时,应该获取相应的ID。
答案 0 :(得分:2)
听起来您需要覆盖自定义对象上的toString()
。对象引用来自toString()
Object
方法
public class StringWithTag{
public Object itemId;
public String itemName;
public StringWithTag(Object itemId,String itemName){
this.itemId = itemId;
this.itemName = itemName;
}
@Override
public void toString() {
return this.itemName;
}
}
答案 1 :(得分:1)
为您的班级创建Setter和Getters
public class StringWithTag {
public String itemId;
public String itemName;
public StringWithTag(String itemId,String itemName){
this.itemId = itemId;
this.itemName = itemName;
}
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
}
并在您的微调器上应用侦听器
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int id = instituteArrayList.get(position)getItemId();
}
});