组合框lwuit的关键价值?

时间:2013-11-20 07:48:38

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

我想存储和检索与comboBox关联的键值。我只用过 getSelectedIndex()和getSelectedItem()。这对我的目的没有帮助,因为我必须获得与该项目相关联的唯一键值。

示例场景:

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

当选择印度时,我需要将价值定为10,如果是中国15,如果是俄罗斯18。

如何在Lwuit 1.5中实现这一目标。你能指导我做这件事。

1 个答案:

答案 0 :(得分: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);
      }
    });
相关问题