如何设置索引“i”处的项目作为 zk 列表框中的选定项目

发布于 2024-09-14 22:08:54 字数 707 浏览 3 评论 0原文

我使用 ListModelList 将选项列表添加到 ZK 列表框。接下来,我尝试遍历这些选择列表并找到所需的项目(例如“字符串”)。我需要将此项目(“字符串”)设置为所选项目。

我尝试了下面的代码,但它不起作用。有办法做到这一点吗?

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

谢谢, 索尼

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不喜欢何必死缠烂打 2024-09-21 22:08:54

如果此代码是您的原始代码,请复制并粘贴到编辑器中,然后删除 if 表达式后面的分号并使用 equals 测试字符串是否相等。 for 循环应如下所示:

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

如果这仍然不起作用,请添加一些调试代码来检查 getValue() 是否确实返回正确的值:

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

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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文