1

What's the best-approach to swap to elements in a Flex Array Collection?

I am binding a ArrayCollection as a dataprovider to combo-box. Selecting a row, should move the object to the top of the combo-box list, and move the top-object to selected object's position.

Satish
  • 6,457
  • 8
  • 43
  • 63

3 Answers3

7

I would do this instead:

dataProvider.addItemAt(dataProvider.removeItemAt(selectedIndex), 0);

The only problem is that this would make the combobox rebind twice, but for simplicity sake it shouldn't be an issue.

J_A_X
  • 12,857
  • 1
  • 25
  • 31
0

Tried setItemAt?

Assaf Lavie
  • 73,079
  • 34
  • 148
  • 203
0

This worked!

      var temp:Object = myDataProvider.getItemAt(0);
      var pos:int = myDataProvider.getItemIndex(selected);

      myDataProvider.setItemAt(selected,0);
      myDataProvider.setItemAt(temp,pos);
      myDataProvider.refresh();
Satish
  • 6,457
  • 8
  • 43
  • 63
  • BUT, this will change items themselves! So, if you referenced on some item of this array collection after this operation its properties will be changed. This is not correct way of swapping items in a list, but this is a way to swap 2 items properties. – radistao Oct 17 '12 at 08:15