Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    SelectItem setValueMap

    Hi there,
    I have a selectItem which I want to add item to the list and remove item from the list.
    I have set the setDefaultToFirstOption to true. The item to be removed is the selected item and after removing the item from an String array, I set the Value map of the selectItem to the new array. However, despite changing the string array as the value map, the selected item (or the removed item) is still inside the selectItem as the displayed (selected) item.
    here is the summary of the code. Any help is much appreciated. I also had to write the array to ArrayList convert methods, since by using library methods such as Arrays.asList() I had strange problems loading the module.

    Code:
    String[] sitesArray = {"a", "b", "c", "d"};
    sitesComboBox = new SelectItem();
    sitesComboBox.setTitle("select");		
    selectedSite = sitesArray[0];
    sitesComboBox.setValueMap(sitesArray);
    sitesComboBox.setDefaultToFirstOption(true);
    sitesComboBox.addChangeHandler(new ChangeHandler(){
    			@Override
    			public void onChange(ChangeEvent event) {				
    				selectedSite = (String)event.getValue();				
    				
    			}		
    		});
    
    sitesForm = new DynamicForm();		
    sitesForm.setWidth(180);		
    sitesForm.setFields(sitesComboBox);
    here is the click handler for the remove site button
    Code:
    removeSiteButton.addClickHandler(new ClickHandler() {			
    	@Override
    	public void onClick(ClickEvent event) {
    		String selectedValue = sitesComboBox.getDisplayValue();
    		ArrayList<String> siteList = convertArrayToList(sitesArray);
    		if(siteList.contains(selectedValue)){						
    			int index = siteList.indexOf(selectedValue);
    			siteList.remove(index);						
    			sitesArray = convertListToArray(siteList);
    			sitesComboBox.setValueMap(sitesArray);
    			sitesComboBox.redraw();					    
                            sitesComboBox.setDefaultToFirstOption(true);				
    			sitesForm.redraw();
    		}
            }
    });
    
    private ArrayList<String> convertArrayToList(String[] stringArr){
    	ArrayList<String> arrayList = new ArrayList<String>();
    	for (int i=0; i<stringArr.length; i++){
    	        arrayList.add(i, stringArr[i]);
    	}
    	return arrayList;
    }
    	
    private String[] convertListToArray(ArrayList<String> list){
    	int size = list.size();
    	String[] stringArr = new String[size];
    	for(int i=0; i<size; i++){
    			stringArr[i] = list.get(i);
    	}		
    	return stringArr;
    }

    #2
    That is expected behavior. I better option is to explicitly set the selected value. You already have the list of strings, so setValue(myList[0]) should be used. If you had a databound selectitem instead it would work more like you expected.

    Comment


      #3
      Thanks very much,
      That fixed the problem

      Comment

      Working...
      X