PDA

View Full Version : list.getNumItems() not working


hoenie
10-25-2006, 01:25 AM
Hi, I am having problem with using getNumItems() in a JavaScript.

I have populated a list with four items using addItem() in JavaScript. Then I created a text view to indicate the number of item in the list. But it is always showing '0', both in the text view and the debug window. What's wrong with getNumItems()?


<canvas height="400">
<include href='lz/textlistitem.lzx'/>
<view height="300" width="200" x="10" y="10">
<simplelayout axis="y"/>
<list id="pool" itemclassname="textlistitem"/>
<text id="count" text="Ready!"/>
</view>

<script>
<![CDATA[
var i;
var names = ["John", "Mary", "Peter", "Henry"];
for (i=0; i<4; i++) {
pool.addItem(names[i], i);
}
Debug.write(pool.getNumItems());
count.setText(pool.getNumItems());
]]>
</script>
</canvas>


The funny thing is that if I type "pool.getNumItems()" in the debug window, it actually evaluates to 4. It just doesn't work in JavaScript.

Any idea? Thanks.

senshi
10-25-2006, 02:16 PM
Well, a list object, or at general a baselist object, uses internally an instance of (data)listselector to retrieve its number of items.
But this (data)listselector object will be created in baselist's init()-method and you try to get the number of items before the init()-method is called. (just read the Debug's output)

So you have two possibilities:
1.: use pool.interior.content.subviews.length (just works with list, not with baselist!)
2.: listen for the list's oninit-Event and add your items after it's send

I would strongly prefer proposal 2, because you don't have to work with the internals of list.


<canvas height="400">
<include href='lz/textlistitem.lzx'/>

<view height="300" width="200" x="10" y="10">
<simplelayout axis="y"/>
<list id="pool" itemclassname="textlistitem" oninit="Debug.write('oninit')" />
<text id="count" text="Ready!"/>
</view>

<script>
<![CDATA[
Debug.write( "we are before init" );

var i;
var names = ["John", "Mary", "Peter", "Henry"];
for (i=0; i<4; i++) {
pool.addItem(names[i], i);
}

Debug.write( pool._selector );//this is undefined
Debug.write( pool.interior.content.subviews.length );//we have got some items

Debug.write(pool.getNumItems());//won't work
count.setText(pool.getNumItems());//won't work
]]>
</script>
</canvas>

hoenie
10-27-2006, 12:00 AM
Thank you, senshi. I think I understand your explanation to a certain degree. I thought the list had already been init'ed when the <list> tag was run, but apparently not.

According to your explanation, I have moved the one-line script to getNumItems into the <list> tag, under an "oninit" method:

<list id="pool" itemclassname="textlistitem"
oninit="setText(pool.getNumItems())"/>

Now, getNumItems() is working fine.

Incidentally, your suggestion also solved another problem I have been having. I was trying to set the canvas background color using javascript to a javascript variable. But it didn't work -- the canvas stay white!

<canvas id="paper">
<script>
paper.setBGColor(0xff3399);
</script>
</canvas>

It turns out that if I move the one-line script into the cavas tag under an oninit method, it will work.

<canvas id="paper" oninit="setBGColor(0xff3399)">
</canvas>

I guess the canvas has not actually been completed by the time the script was run. I don't understand why this is the case but at least I know how to get it to work when something like this happens in the future.

Thank you, once again.