For loop Count PLEASE HELP

You must Login before you can answer or comment on any questions.

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

— asked 7 months ago by Stiaan Maas
2 Comments
  • Rather than alerting ID, what is e.source.ID?

    — commented 7 months ago by Mark Henderson

  • sorry I don't understand what you are asking ?

    — commented 7 months ago by Stiaan Maas

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);
});

— answered 7 months ago by Moritz Knecht
answer permalink
10 Comments
  • sorry, i forgot that you have to tag the id in your row first

    row = Ti.UI.createTableViewRow(
        {
            top:'12%',
            id:i,
            height:100,
        });

    — commented 7 months ago by Moritz Knecht

  • Um i don't fully understand what is happening in that what is the e.row.id ... Sorry I'm very new to this

    — commented 7 months ago by Stiaan Maas

  • OK now i get it I'll try that thanx

    — commented 7 months ago by Stiaan Maas

  • Show 7 more comments

Your Answer

Think you can help? Login to answer this question!