Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Label Click Event Listener Problem

Hey Guys

I have a slight issue that is really bugging me, I am creating a load of Labels on the fly based on the results of a JSON query (Contained in a For Loop).

I have an event listener that sits outside the For Loop, this then goes to a different window.

The problem is that it is sending the data contained in the last label into the new window rather than the label that was clicked. This is rather frustrating and I cant for the life of me figure out how to make it work with the label that was clicked.

Here is my code, any ideas?

for (var i in events)
{    
Ti.API.info("Adding event for venue: " + events[i].name);

var alteredName = events[i].name.replace("@ " + events[i].location, "");

Ti.API.info(alteredName);

var tempLabel = Titanium.UI.createLabel(
{
text:alteredName,
height:50,
top: top,
left:5,
width:'auto',
color:'white',
font:{fontSize:15, fontStyle:'normal',fontWeight:'bold'},
textAlign:'left',
fbId: events[i].id,
fbName: events[i].name
});

tempLabel.addEventListener('click', function(e) {
var eventDetailWin = Titanium.UI.createWindow({    
url:'kfNightZoom.js'
});
Ti.API.info("About to output event sender details");
Ti.API.info(e);
                                            Ti.API.info(e.source);
var eventFBID = events[i].id;
var eventName = events[i].name;

eventDetailWin.eventName = eventName;
eventDetailWin.eventFBID = eventFBID;

Titanium.UI.currentTab.open(eventDetailWin,{animated:true});
});

top = top + 55;
scrollView.add(tempLabel);
actInd.hide();
}
— asked November 28th 2010 by Justin Howard
  • label
  • listener
0 Comments

3 Answers

  • Accepted Answer

    Justin

    Yes, sorry I didn't notice it before, but events[i] won't of course be available in the eventListener. You'd need to obtain the data from your custom properties (that you are already setting, presumably for this purpose), ie e.source.fbId, e.source.fbName, or the standard properties, ie e.source.text.

    This is why you are always getting the data contained in the last events[i], as this was the last value when the loop ended.

    — answered November 28th 2010 by Paul Dowsett
    permalink
    0 Comments
  • Hi Justin

    Try replacing:

    for (var i in events)
    

    with:

    for (var i = 0, ilen=events.length; i<ilen; i++)
    
    — answered November 28th 2010 by Paul Dowsett
    permalink
    1 Comment
    • Hi

      I have tried that, but I now get the following error:
      message = "Result of expression 'events[i]' [undefined] is not an object.";

      Which relates to this line of code

      var eventFBID = events[i].id;
      

      It is weird because the previous lines work ok, and it is still contained within the For loop Scratches Head

      — commented November 28th 2010 by Justin Howard
  • Justin

    If you return to your original code, what is the value of "i" for each iteration?

    To find this, at the top of your loop and again at the top of your event listener put:

    Ti.API.info('i = '+i);
    
    — answered November 28th 2010 by Paul Dowsett
    permalink
    2 Comments
    • Hi Hal

      It comes back with the number of each result, so 1,2,3,4 etc depending on the amount of results. This is why I am confused as to why it doesn't like this

      events[i].id;
      

      — commented November 28th 2010 by Justin Howard
    • I have just checked and events is till populating correctly in the event listener, it just seems that since I popped that new code in Hal, that [i] is no longer able to be used

      — commented November 28th 2010 by Justin Howard
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.