【问题标题】:Key Value in combobox lwuit?组合框lwuit中的键值?
【发布时间】:2013-11-20 07:48:38
【问题描述】:

我想存储和检索与组合框关联的键值。我只用过 getSelectedIndex() 和 getSelectedItem()。这无助于我的目的,因为我必须获得与该项目相关的唯一键值。

示例场景:

印度 - 10,中国 - 15,俄罗斯 - 18。这里如果“印度”是组合框项目,那么“10”是它的键。同样,中国为 15,俄罗斯为 18。

When India is selected I need to get the value as 10, if china 15, if Russia 18.

如何在 Lwuit 1.5 中实现这一点。你们能指导我这样做吗?

【问题讨论】:

    标签: java-me lwuit lwuit-list lwuit-combobox


    【解决方案1】:

    我认为您必须将值与ComboBox 中的项目映射。

    您可以通过一些不同的方式做到这一点。

    例如,您可以使用Hashtable 进行操作。您需要进行正确的强制转换才能获得所需的数据类型中的值。

        ComboBox combo;
    
        //Here create the hash
        Hashtable h = new Hashtable();
        h.put("India", "10");
        h.put("China", "15");
        h.put("Russia", "18");
    
        //create the combo
        Vector v = new Vector();
        v.addElement("India");
        v.addElement("China");
        v.addElement("Russia");
        combo = new ComboBox(v);
        combo.addActionListerner(new ActionListener ae){
          public void actionPerformed(ActionEvent ae){
                 String selected = (String) combo.getSelectedItem();
                 //get the value
                 String value = (String) h.get(selected);
          }
        });
    

    【讨论】:

    • 谢谢 jmunoz,正是我要找的东西
    • 不客气。如果您可以投票,那就太好了!
    猜你喜欢
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 2015-11-11
    • 1970-01-01
    • 2012-03-21
    相关资源
    最近更新 更多