1
votes

When setting an arrayCollection as a dataProvider to a comboBox programmatically,if the arrayCollection has just one element,i need to do a small validation:

> public resultHandler(event:ResultEvent):void{

   arrColl = event.result.FlexData.ListData as ArrayCollection; 

//to check if the arrColl has only one element 

if(arrColl == null)
      myComboBox.dataProvider = event.result.FlexData.ListData

else

 myComboBox.dataProvider = arrColl;

}

I would like to know,if there is a way to skip this validation every time.Is there a way to set dataProvider such that i dont have to check if the collection has one or more elements?

1
I'm a little confused here. It looks like no matter what you gets returned, you're binding the dataProvider to event.result.FlexData.ListData. The only thing that's different is whether you're binding to an ArrayCollection or whatever object is returned in event.result.FlexData.ListData. I'm not seeing where you check to see if there's only one element or not. Is there a reason you don't just return an ArrayCollection of one item if only one item is returned? It would keep you from having to do this check.Jason Towne
@jason<FlexData> node in the XML returned from server has one or more<ListData> nodes.So binding to ListData as ArrayCollection serves my purpose.If the arrColl has only one element it still is null..When you do the null validation ,you are actually checking if it has one element.I think christopher's answer explains it and serves my purpose.Thanks.himanshu

1 Answers

3
votes

There is no built-in way to do this.

You'll need to either:

  • create a utility method that does this. For instance

    myComboBox.dataProvider = ComboBoxUtil.setDataProvider(collection);
    
  • subclass the ComboBox control and override the dataProvider setter where you can include this logic