0

I am just confused. I am trying to get the value of the label in a select list. I tried this:

int approverID = 1;

for(SelectItem s : approverList) {
       if(s.getValue().equals(approverID)) {
              selectedBlockingConditionRule.setApproverName(s.getLabel());
       }
}

and it worked. But when I tried to simplify it and used this:

int approverIndex = this.approverList.indexOf(approverID);
String approverName = this.approverList.get(approverIndex).getLabel();

it returns -1.

I also tried to cast approverID to both Object and Integer but still got -1 for the index. Can you guys help me figure out why this is happening? :(

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
roro
  • 11
  • 3
    Where's `s.getValue()` in 2nd attempt? – BalusC Jan 15 '16 at 07:47
  • index is the place of the item in the list, but approverID should not be compared to the index, it is the value of the item! – Gavriel Jan 15 '16 at 07:59
  • 6
    you can use this aproach: http://stackoverflow.com/questions/13138990/how-to-search-in-a-list-of-java-object or http://www.leveluplunch.com/java/examples/find-element-in-list/ – Gavriel Jan 15 '16 at 08:01
  • another possiblility is to store the items in a HashMap instead of a list, where the key is the approverId, then you can get the desired item: map.get(approverID) – Gavriel Jan 15 '16 at 08:03

0 Answers0