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();
}
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.
-
Hi Justin
Try replacing:
for (var i in events)
with:
for (var i = 0, ilen=events.length; i<ilen; i++)
-
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);