DropDown (Select) issue...

Hi all,

I have a drop down (a Select) which I fill some TASKS into it as follows.

private Select drpTask = new Select();

private void addTasksToDropdown(ArrayList<Task> tasks){
if(tasks == null || tasks.isEmpty()) return;
		
  for(Task task : tasks){
    drpTask.setItemCaption(task.getTaskId(), task.getTaskName()); [color=#009413]
// Set the task id as the ID & the task name as the caption
[/color]
    drpTask.addItem(task.getTaskName());
  }
}

I can get the Task Name as

String taskName = (String)drpTask.getValue();

But is there a way for me to get the TASK ID(of the selected task) by using the TASK NAME…??
[something like
int taskId = drpTask.getItemID(task.getTaskName())
]

If there’s no such a way, can someone please suggest me a workaround…??

Any help is greatly appreciated.

Thanks in advance,
Asela.

You should set the task ID as the item ID, not the name.

This way:

drpTask.addItem(task.getTaskId());
drpTask.setItemCaption(task.getTaskId(), task.getTaskName());

Also, the setItemCaption() doesn’t “set the ID”, but uses it as a key. Setting the item caption should be done after adding the item. Amazingly it doesn’t fail in your code. Perhaps it should, but having to handle the exception would not be nice.