PDA

View Full Version : Add items to combobox store multiple times



stigrv
11 Mar 2014, 12:26 AM
When the store connected to a ComboBox are cleared and re-filled, you might run into situations where the ListView fails while adding items. This is illustrated by the following example:



public class ListViewMultiAdd implements EntryPoint {


@Override
public final void onModuleLoad() {
final ListStore<String> store = new ListStore<>(new ModelKeyProvider<String>() {
@Override
public String getKey(final String item) {
return item;
}
});


final ComboBox<String> comboBox = new ComboBox<>(store, new LabelProvider<String>() {
@Override
public String getLabel(final String item) {
return item;
}
});
RootPanel.get().add(comboBox, 10, 10);


final TextButton add = new TextButton("Add items");
add.addSelectHandler(new SelectHandler() {
@Override
public void onSelect(final SelectEvent event) {
store.clear();
store.addAll(Arrays.asList("Test 1", "Test 2"));
store.addAll(Arrays.asList("Test 3", "Test 4"));
}
});
RootPanel.get().add(add, 200, 10);
}
}


Press "Add items"
Open combobox
Press "Add items" again
See that ListView produces an error
See that the combobox has the two first items only

vardhan22
12 Mar 2014, 1:39 AM
Clear the ComboBox as well in onSelect. It will work