Reputation: 35346
Our gwt app uses ValueListBox
,
records.setValue(model);
records.setAcceptableValues(models);
Where model
is a subset of models
. The problem I'm having is that the ValueListBox contains duplicated value. How do I prevent or remove it?
Upvotes: 2
Views: 571
Reputation: 46871
Override setAcceptableValues
methods
@Override
public void setAcceptableValues(Collection<T> newValues) {
super.setAcceptableValues(new HashSet<T>(newValues));
}
Sample code:
ValueListBox<Integer> semester = new ValueListBox<Integer>(new Renderer<Integer>() {
public String render(Integer object) {
String s = "";
if (object != null) {
s = object.toString();
}
return s;
}
public void render(Integer object, Appendable appendable) throws IOException {
String s = render(object);
appendable.append(s);
}
}) {
@Override
public void setAcceptableValues(Collection<Integer> newValues) {
super.setAcceptableValues(new HashSet<Integer>(newValues));
}
};
semester.setAcceptableValues(Arrays.asList(1, 1, 3, 3, 2, 2, 3, 4, 5, 7, 8, 9, 1, 3, 2));
semester.setValue(2);
--EDIT--
As per your last comment try this one
class MyModel {
private String id;
private String name;
public MyModel(String id, String name) {
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
return this.id.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof MyModel) {
return this.id.equals(((MyModel) obj).id);
}
return false;
}
}
ValueListBox<MyModel> semester = new ValueListBox<MyModel>(new Renderer<MyModel>() {
public String render(MyModel object) {
String s = "";
if (object != null) {
s = object.getName();
}
return s;
}
public void render(MyModel object, Appendable appendable) throws IOException {
String s = render(object);
appendable.append(s);
}
}) {
@Override
public void setAcceptableValues(Collection<MyModel> newValues) {
super.setAcceptableValues(new HashSet<MyModel>(newValues));
}
};
semester.setAcceptableValues(Arrays.asList(new MyModel("1", "a"), new MyModel("1", "a"),
new MyModel("2", "b"), new MyModel("3", "c")));
semester.setValue(new MyModel("1", "a"));
Upvotes: 2
Reputation: 460
Try using the below.
records.setAcceptableValues(new HashSet<String>(models);
If it is a user defined object other than string etc, then you have to implement hashcode and equals.
Upvotes: 4
Reputation: 41099
There is no method to prevent or remove duplicate options in ListBox or ValueListBox. You have to remove duplicate values before you pass them to your ValueListBox.
Upvotes: 0