簡體   English   中英

GWT列表框未填充

[英]GWT listbox not getting populated

我正在使用gwt 2.6.0。 我的頁面上有一個列表框,使用以下代碼填充

    public void setOHIInfo(ArrayList<OHIInfoTo> result) {
    ohiListBox.setEnabled(true); // ohiListBox is instance of com.google.gwt.user.client.ui.ListBox
    for (OHIInfoTo ohiinfo : result) {
        ohiListBox.addItem(ohiinfo.getOhiName(), ohiinfo.getOhiName());
        GWT.log(ohiinfo.getOhiName()); // 5 data items are printed; no spaces
    }
    GWT.log("OHI Count - " + ohiListBox.getItemCount()); // Prints OHI Count - 5
}

頁面加載時,將打印2條GWT.log行,但是列表框為空白。 我已經在項目中搜索,沒有其他地方可以覆蓋列表框。 我還檢查了Chrome中的listbox元素,實際上它沒有任何元素

還有其他方法可以找到問題所在嗎?

愚蠢的問題:您是否在以下示例中將列表框添加到了gwt入口點likie中加載的任何容器中?

public class ListBoxExample implements EntryPoint {

  public void onModuleLoad() {
    // Make a new list box, adding a few items to it.
    ListBox lb = new ListBox();
    lb.addItem("foo");
    lb.addItem("bar");
    lb.addItem("baz");
    lb.addItem("toto");
    lb.addItem("tintin");

    // Make enough room for all five items (setting this value to 1 turns it
    // into a drop-down list).
    lb.setVisibleItemCount(5);

    // Add it to the root panel.
    RootPanel.get().add(lb);
  }
}

編輯 :我剛剛嘗試過這種方式的代碼:

public class OHIInfoTo {

    String name;

    public OHIInfoTo(String name) {
        this.name = name;
    }

    public String getOhiName() {
        return name;
    }

    public void setOhiName(String name) {
        this.name = name;
    }
}

public class ListBoxEP implements EntryPoint {
    @Override
    public void onModuleLoad() {
        List<OHIInfoTo> result = new ArrayList<OHIInfoTo>();
        result.add(new OHIInfoTo("toto"));
        result.add(new OHIInfoTo("hello"));

        ListBox ohiListBox = new ListBox(false);
        ohiListBox.setEnabled(true); // ohiListBox is instance of
                                        // com.google.gwt.user.client.ui.ListBox
        for (OHIInfoTo ohiinfo : result) {
            ohiListBox.addItem(ohiinfo.getOhiName(), ohiinfo.getOhiName());
            // GWT.log(ohiinfo.getOhiName()); // 5 data items are printed; no
            // spaces
        }

        ohiListBox.setVisibleItemCount(ohiListBox.getItemCount());

        RootPanel.get("editorAppContainer").add(ohiListBox);
    }
}

而且效果很好。 您確定沒有其他可能導致此問題的東西嗎? 調用setVisibleItemCount時,listBox是否已填充?

終於解決了這個! 對setOHIInfo的調用是異步的。 因此,此方法幾乎與構造函數並行執行,並在重新整理列表框之前完成。 不知何故,它沒有引發空指針異常。 那仍然是一個謎。

要修復-我確保首先呈現列表框,然后設置值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM