简体   繁体   中英

How to save id's other than keyword in SuggestBOX?

I am using a simply city SuggestBox where I am getting list of cities from the database and putting them in GWT suggestBox oracle.

After that user can select his city from the suggestBox suggestions and user saves his record. For example, he will select "London" from the suggestbox list.

Now when user saves his record, I will not save "London" in the database for that user, instead I want to save "3" (london ID) in database.

For this what I am doing is like this:

       public MultiWordSuggestOracle createCitiesOracle(ArrayList<City> cities){
    for(int i=0; i<cities.size(); i++){
        oracle.add(cities.get(i).getCity()+","+cities.get(i).getCityId());

    }
    return oracle;
}

Now, I have the city and cityID both displaying in suggestBox and then can save from there 'city' and 'cityId'.

Everything works fine, but it doesn't looks good:
在此处输入图片说明

like it dispays as "London,3" and so on in the suggestBox suggestions.. I don't want to show this 3, how and where can I save this Id(3) for future use?

You can also create your own typed Suggestion-Box. You need to implement "Suggestion" and extend "SuggestOracle".

Super simple version may look:

// CityOracle 
public class CityOracle extends SuggestOracle {

  Collection<CitySuggestion> collection;

  public CityOracle(Collection<CitySuggestion> collection) {
        this.collection = collection;
  }

  @Override
  public void requestSuggestions(Request request, Callback callback) {
    final Response response = new Response();

    response.setSuggestions(collection);
    callback.onSuggestionsReady(request, response);
  }

}



//CitySuggestion
public class CitySuggestion implements Suggestion, Serializable, IsSerializable {

    City value;

    public CitySuggestion(City value) {
        this.value = value;
    }

    @Override
    public String getDisplayString() {
        return value.getName();
    }

    @Override
    public String getReplacementString() {
        return value.getName();
    }

    public City getCity() {
        return value;
    }

}


 // Usage in your code:

// list of cities - you may take it from the server 
List<City> cities = new ArrayList<City>();
cities.add(new City(1l, "London"));
cities.add(new City(2l, "Berlin"));
cities.add(new City(3l, "Cracow"));

// revert cities into city-suggestions
Collection<CitySuggestion> citySuggestions = new ArrayList<CitySuggestion>();
for (City city : cities) {
    citySuggestions.add(new CitySuggestion(city));
}

//initialize city-oracle
CityOracle oracle = new CityOracle(citySuggestions);

// create suggestbox providing city-oracle     
SuggestBox citySuggest = new SuggestBox(oracle);

// now when selecting an element from the list, the CitySuggest object will be returned. This object contains not only a string value but also represents selected city
citySuggest.addSelectionHandler(new SelectionHandler<SuggestOracle.Suggestion>() {

    @Override
    public void onSelection(SelectionEvent<Suggestion> event) {
        Suggestion selectedItem = event.getSelectedItem();
        //cast returned suggestion
        CitySuggestion selectedCitySuggestion = (CitySuggestion) selectedItem;
        City city = selectedCitySuggestion.getCity();
        Long id = city.getId(); 
    }
});

Map<String, Integer>中将引用从城市名称保留到ID,然后在保存之前在该ID上查找。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM