Tell me, please, addSelectionHandler add-in

I have a custom Oracle with objects to go to the tooltip. Then I need to return the object when it is selected from the tooltip.

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

The problem is that I have no suggestion. I have a "CustomSuggestion". I read the API, and I'm trying to write a Custom SuggestBox that implements the HasSelectionHandlers interface, but I cannot, because SuggestBox has an interface implementation. I get an error message:

The interface HasSelectionHandlers cannot be implemented more than once with different arguments: HasSelectionHandlers<SuggestOracle.Suggestion> and HasSelectionHandlers<CustomSuggestion>

Could you help me? Sorry for my bad english.

+3
source share
1 answer

, . ( , , ). , :

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

Source: https://habr.com/ru/post/1779769/


All Articles