2010-12-11 59 views
3

我有一個自定義的Oracle對象傳遞給SuggestBox。然後,我需要從de SuggestBox中選擇一個對象。SuggestBox重寫addSelectionHandler

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

問題是我沒有建議。我有「CustomSuggestion」。我閱讀de API,並嘗試編寫實現接口HasSelectionHandlers的自定義SuggestBox,但是我不能,因爲SuggestBox具有該接口的實現。我收到錯誤:

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

你能幫我嗎?對不起,我的英語不好。

回答

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

嗨。我決心沒有演員,但我改變它,現在工作。謝謝。 String value =((CustomSuggestion)event.getSelectedItem())。fSomeOtherValue; – theleftone 2010-12-15 08:53:01

+0

它在javadoc中表示「將'Suggestion'對象向下轉換爲子接口」。猜猜它是有意的。 – z00bs 2010-12-15 09:11:58