tags:

views:

109

answers:

1

Hi,

I added a list of choices using ListModelList to a ZK listbox. Next, I tried to loop through these list of choices and find a required item (say "String"). I need to set this item ("String") as the selected item.

I tried the code below but it doesn't work. Is there a way to do this ?

  liveListModel = new ListModelList(new AppModelItem [] { 
        new AppModelItem("String", "string"), 
        new AppModelItem("Number", "number"), 
        new AppModelItem("Array", "array")
    });

    String choice [] = {"String", "Hello", "XYZ" };

    Listbox typesList = new Listbox();
    typesList.setModel(liveListModel);
    for (int i = 0; i < choice.length; i ++) {
        if (choice.[i] == typesList.getItemAtIndex(i).getValue().toString());
        typesList.setSelectedItem(typesList.getItemAtIndex(i));
    }

Thanks, Sony

A: 

If this code is your original code, copied and pasted to the editor, then remove the semicolon after the if expression and use equals to test Strings for equality. The for loop should look like this:

for (int i = 0; i < choice.length; i++) {
    if (choice[i].equals(typesList.getItemAtIndex(i).getValue().toString())) {
        typesList.setSelectedItem(typesList.getItemAtIndex(i));
    }
}

If this still doesn't work, add some debug code to check if getValue() really returns the correct value:

for (int i = 0; i < choice.length; i++) {
    if (choice[i].equals(typesList.getItemAtIndex(i).getValue().toString())) {
        typesList.setSelectedItem(typesList.getItemAtIndex(i));
    } else {
      // DEBUG CODE
      System.out.printf("Expected: %s, found: %s%n", typesList.getItemAtIndex(i).getValue().toString());
}
Andreas_D
Oh that was a silly mistake I made. Thanks!
sony