Gwt Listbox item reference to an Object
I have got a Listbox and i want to add some Items. There are only methods to add items as a String, but I want add a Item to the Listbox with a String and a reference to an Object. So that, if a item was selected in the Listbox, I get the O开发者_运维问答bject reference too. Otherwise I have always to search with equal for the right Object.
Is there any option therefore?
Try ValueListBox
instead of ListBox
.
You could store the object in a Map indexed by the value of the item, or in an array or List, at the index of the added item in the list box.
This is an old question, but since the most elegant answer is not yet here:
// Applicable for an object of specified type 'User'
ValueListBox<User> lbUser = new ValueListBox<User>(new Renderer<User>() {
public String render(User user) {
String s = "";
if (user != null) {
// Specify the format for the Strings to display per list item here. In this example, it is
// 'username (firstname lastname)'
// For example: MTielemans (Mark Tielemans)
s = user.getUsername() + "("+user.getFirstname()+" " + user.getLastname()+")";
} else {
s = "Select a user";
}
return s;
}
public void render(User user, Appendable appendable) throws IOException {
String s = render(user);
appendable.append(s);
}
});
精彩评论