Hi all, I have a for loop that populates to the amount of files in a Json and I give each item an ID in the for loop so if i click on one the id will display of the specific one but it only displays the last ID in the loop
var lblTaskName; var url = 'https://raw.github.com/appcelerator/Documentation-Examples/master/HTTPClient/data/json.txt'; var json , Task , i , row; var table = Ti.UI.createTableView({ top:'11%',bottom:'11%'}); var tableData = []; var xhr = Ti.Network.createHTTPClient({ onload: function() { json = JSON.parse(this.responseText); for ( i = 0; i < json.fighters.length ; i++){ Task = json.fighters[i]; row = Ti.UI.createTableViewRow( { top:'12%', height:100, }); lblTaskName = Ti.UI.createLabel( { text:Task.name, font:{fontSize:'24dp'}, height:'auto', left:'1%', top:'10%', color:'#000', nameID:i, touchEnabled:true, }); lblTaskName.addEventListener('singletap',Validate) row.add(lblTaskName); tableData.push(row); } table.setData(tableData); },and the validate Function is
function Validate(e) { var ID = lblTaskName.nameID; alert(ID); //Rest of code will follow but need ID first };And it always displays the last ID of the loop
1 Answer
Accepted Answer
As far is I understand, don't set event listener for each label in a row, set only one listener for your tableview instead.
table.addEventListener('click', function(e) { Ti.API.info("e.row.id: "+e.row.id); });
Your Answer
Think you can help? Login to answer this question!