SuggestBox覆盖addSelectionHandler

时间:2010-12-11 19:36:52

标签: gwt

我有一个带有Objects的自定义Oracle传递给SuggestBox。然后我需要从de SuggestBox中选择一个对象。

public HandlerRegistration addSelectionHandler(SelectionHandler<SuggestOracle.Suggestion> handler)

问题是我没有建议。我有“CustomSuggestion”。我读了de API,我尝试编写一个实现HasSelectionHandlers接口的Custom SuggestBox,但我不能,因为SuggestBox有一个接口的实现。我收到错误:

The interface HasSelectionHandlers cannot be implemented more than once with different arguments: HasSelectionHandlers<SuggestOracle.Suggestion> and HasSelectionHandlers<CustomSuggestion>
你能帮帮我吗?抱歉我的英语不好。

1 个答案:

答案 0 :(得分:6)

不确定我理解你的问题。看看下面的例子(非常基本,但你应该知道如何处理自定义建议)。希望有所帮助:

public void onModuleLoad() {
    SuggestBox box = new SuggestBox(new CustomOracle<CustomSuggestion>());

    box.addSelectionHandler(new SelectionHandler<SuggestOracle.Suggestion>() {

        @Override
        public void onSelection(SelectionEvent<Suggestion> event) {
            String value = ((CustomSuggestion) event.getSelectedItem()).fSomeOtherValue;
            Window.alert(value);
        }
    });
    RootPanel.get().add(box);
}

private class CustomOracle<CustomSuggestion> extends SuggestOracle {

    private LinkedList<Starter.CustomSuggestion> fStore;

    public CustomOracle() {
        fStore = new LinkedList<Starter.CustomSuggestion>();
        fStore.add(new Starter.CustomSuggestion("2", "two", "foo"));
        fStore.add(new Starter.CustomSuggestion("22", "twenty-two", "bar"));
        fStore.add(new Starter.CustomSuggestion("222", "two-hundred twenty-two", "w000t"));
    }

    @Override
    public void requestSuggestions(Request request, Callback callback) {
        String query = request.getQuery();
        LinkedList<Starter.CustomSuggestion> result = new LinkedList<Starter.CustomSuggestion>();
        for (Starter.CustomSuggestion entry : fStore) {
            if (entry.fDisplay.contains(query)) {
                result.add(entry);
            }
        }
        callback.onSuggestionsReady(request, new Response(result));
    }

}

private class CustomSuggestion implements Suggestion {

    private String fReplace;
    private String fDisplay;
    private String fSomeOtherValue;

    public CustomSuggestion(String display, String replace, String someOtherValue) {
        fDisplay = display;
        fReplace = replace;
        fSomeOtherValue = someOtherValue;
    }

    @Override
    public String getDisplayString() {
        return fDisplay;
    }

    @Override
    public String getReplacementString() {
        return fReplace;
    }
}