sony
sony

Reputation: 1587

how to set an item at index "i" as selected item in zk listbox

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

Upvotes: 0

Views: 6384

Answers (1)

Andreas Dolk
Andreas Dolk

Reputation: 114767

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

Upvotes: 1

Related Questions