Search code examples
java-melwuitlwuit-listlwuit-combobox

Key Value in combobox lwuit?


I want to store and retrieve a key value associated with the comboBox. I have used only getSelectedIndex() and getSelectedItem(). This will not help my purpose as I have to get a unique key value assocaiated with the item.

Example Scenario :

India - 10, China - 15, Russia - 18. Here if 'India' is the comboBox item then '10' is its key. Likewise, 15 for china and 18 for Russia.

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

How can I achieve this in Lwuit 1.5. could you guys guide me to do this.


Solution

  • I think that you must map the values with the items in the ComboBox.

    You can do this in some different ways.

    You can do it with a Hashtable for example. You will need to make the proper castings to get the value in the data type what you want.

        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);
          }
        });