PDA

View Full Version : ArrayCollection removeAll problem


doorman
10-06-2008, 02:12 PM
Hi,

I am using Flex 3 and I have a DataGrid that is bound to an ArrayCollection.
When I sort any of the columns and call removeAll to clear the datagrid I get the following error:

RangeError: Index '-1' specified is out of bounds.
at mx.collections::ArrayList/removeItemAt()
at mx.collections::ListCollectionView/removeItemAt()
at mx.collections::ListCollectionView/removeAll()
at pages::Inputs/fillGrid()
at Function/http://adobe.com/AS3/2006/builtin::apply()
at <anonymous>()
at SetIntervalTimer/onTimer()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

Any suggestions?

Thanks!

Peter Cowling
10-06-2008, 02:55 PM
Code please.

doorman
10-07-2008, 12:01 PM
Thanks for your reply.

This is part of the mxml file. When fillGrid is called all items are removed from columns and then filled again with new items.


<mx:Script><![CDATA[

[Bindable]
private var columns:ArrayCollection;
private var listInputs:ArrayList;

private function fillGrid():void
{
columns.removeAll();
var input:Input;
for (var i:int = 0; i < listInputs.length; i++)
{
input = Input(listInputs.getItemAt(i));
addColumn(input.getID(), input.getStatus(), input.getUpdated(), input.getValue(), input.getHWID(), input.getName(), input.getLocation(),
input.getContext(), input.getInputType(), input.getPort(),
input.getDataRate(), input.getRangeMin(), input.getRangeMax());
}
}

private function addColumn(id:String, status:String, updated:String, value:String, hwid:String, name:String, location:String, context:String,
type:String, port:String,
dataRate:String, rangeMin:String, rangeMax:String):void
{
columns.addItem({ID:id, Status:status, "Updated":updated, Value:value, HWID:hwid,
Name:name, Location:location, Context:context, Type:type, Port:port,
Datarate:dataRate, "Min range":rangeMin, "Max range":rangeMax});
}

]]></mx:Script>

<mx:DataGrid id="gridInputs" width="100%" height="100%" fontSize="14" dataProvider='{columns}' itemClick="focusIn(null);">
<mx:columns>
<mx:DataGridColumn dataField="Status" width="70"/>
<mx:DataGridColumn dataField="Updated" width="120"/>
<mx:DataGridColumn dataField="Value" width="70"/>
<mx:DataGridColumn dataField="HWID" width="70" />
<mx:DataGridColumn dataField="Name" width="120" />
<mx:DataGridColumn dataField="Location" width="120"/>
<mx:DataGridColumn dataField="Context" width="120"/>
<mx:DataGridColumn dataField="Type" width="50" />
<mx:DataGridColumn dataField="Port" width="50"/>
<mx:DataGridColumn dataField="Datarate" width="50" />
<mx:DataGridColumn dataField="Min range" width="50"/>
<mx:DataGridColumn dataField="Max range" width="50"/>
</mx:columns>
</mx:DataGrid>

Peter Cowling
10-07-2008, 06:33 PM
Hi,

I could not get it to reproduce, but then I did not try the repopulate part. Guessing that removeall still does not work if you just try this by itself?

It may well not, because this same thing is listed as a bug on Adobe's site.

Workarounds I can think of are:

1. Make the dataprovider null before clearing out the arraycollection. Then re-specify it (after you have repopulated it in preference). If this is still causing a problem, you could get the length of the arraycollection and remove the items one-by-one. (And if for some mad reason this still does not work, sort, refresh, and then delete them one-by-one.) This approach should at least work.

2. Shore-up the column sorting process. Use a custom sort instead of the [dgrids] header column sort. Frankly, without knowing what the cause of the bug is, there is no way of knowing whether this will do anything at all.

doorman
10-08-2008, 01:32 PM
Thanks Peter,

since the removeAll function is not working I re-initalize the dataprovider:

columns = null
columns = new ArrayCollection()

every time I reset the DataGrid and the problem is gone!

Peter Cowling
10-08-2008, 02:07 PM
Good to know that worked out for you.