【问题标题】:Creating a Spinner from an ArrayList<Object>从 ArrayList<Object> 创建微调器
【发布时间】:2011-10-31 14:53:56
【问题描述】:

I am wanting to create a Spinner from an ArrayList of Objects I have created and therefore when one is selected I can go back to that ArrayList and get the rest of the information from it

例子

public class ObjectName {
    private int ID;
    private String name;
    private String name2;
    public ObjectName{int pID, String pName, String pName2) {
        ID = pID;
        name = pName;
        name2 = pName2;
    }
    //Getters Here
}

微调器代码示例

ArrayList<ObjectName> objects = new ArrayList<ObjectName>;
ArrayAdapter<ObjectName> adapter = new ArrayAdapter<ObjectName>(this, android.R.layout.simple_spinner_item, objects);

当然,微调器不会显示我想要的。有没有简单的方法来解决这个问题

感谢您的宝贵时间

【问题讨论】:

  • 您在哪里将值添加到 ArrayList?
  • 从另一个类创建 Arraylist(从数据库中提取数据)

标签: android


【解决方案1】:

您需要在ObjectName 类中实现toString 方法。否则微调器不知道要显示什么!

public String toString() {
    return ID + " " + name + " " + name2;
}

【讨论】:

  • 需要更换ArrayAdapter Line吗?
【解决方案2】:

你需要像 Caner 所说的那样在你的 ObjectName 类中实现 toString,你还需要为你的微调器设置适配器,使用类似的东西:

((Spinner) findViewById(R.id.mySpinner)).setAdapter(adapter);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-07
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多