gpt4 book ai didi

java - 如何在 SuggestBOX 中保存除关键字以外的 id?

转载 作者:行者123 更新时间:2023-12-02 05:33:43 25 4
gpt4 key购买 nike

我正在使用一个简单的城市 SuggestBox,我从数据库中获取城市列表并将它们放入 GWT suggestBox oracle 中。

之后,用户可以从 suggestBox 建议中选择他的城市,并且用户保存他的记录。例如,他将从建议框列表中选择“伦敦”。

现在,当用户保存他的记录时,我不会在该用户的数据库中保存“伦敦”,而是想在数据库中保存“3”(伦敦 ID)。

为此,我正在做的是这样的:

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

现在,我的城市和城市 ID 都显示在 suggestBox 中,然后可以从那里保存“city”和“cityId”。

一切正常,但看起来不太好:
enter image description here

就像它在 suggestBox 建议中显示为“London,3”等等。我不想显示此 3,如何以及在哪里保存此 Id(3) 以供将来使用?

最佳答案

您还可以创建自己的键入建议框。您需要实现“Suggestion”并扩展“SuggestOracle”。

super 简单的版本可能看起来:

// 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();
}
});

关于java - 如何在 SuggestBOX 中保存除关键字以外的 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18523810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com