我有一个带有对象的自定义 Oracle,可以传递给 SuggestBox。然后我需要在从 de SuggestBox 中选择一个对象时取回一个对象。

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

问题是我没有建议。我有“自定义建议”。我阅读了 API 并尝试编写一个实现接口(interface) HasSelectionHandlers 的自定义 SuggestBox,但我不能,因为 SuggestBox 有接口(interface)的实现。我收到错误:
The interface HasSelectionHandlers cannot be implemented more than once with different arguments: HasSelectionHandlers<SuggestOracle.Suggestion> and HasSelectionHandlers<CustomSuggestion>

你能帮助我吗?对不起,我的英语不好。

最佳答案

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

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

关于gwt - SuggestBox 覆盖 addSelectionHandler,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4418557/

10-12 05:53