首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SuggestBox覆盖addSelectionHandler

SuggestBox覆盖addSelectionHandler
EN

Stack Overflow用户
提问于 2010-12-12 03:36:52
回答 1查看 4.5K关注 0票数 3

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

代码语言:javascript
复制
public HandlerRegistration addSelectionHandler(SelectionHandler<SuggestOracle.Suggestion> handler)

问题是我没有建议。我有"CustomSuggestion“这个词。我阅读了de API,并尝试编写一个实现接口HasSelectionHandlers的自定义SuggestBox,但我做不到,因为SuggestBox有该接口的实现。我得到了错误:

代码语言:javascript
复制
The interface HasSelectionHandlers cannot be implemented more than once with different arguments: HasSelectionHandlers<SuggestOracle.Suggestion> and HasSelectionHandlers<CustomSuggestion>

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-15 00:44:53

我不太明白你的问题。看一下下面的例子(非常基础,但你应该对如何处理自定义建议有所了解)。希望这能有所帮助:

代码语言:javascript
复制
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;
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4418557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档