钛移动:从按钮点击问题的tableview获取行值

时间:2012-02-16 05:43:02

标签: titanium-mobile

朋友你好,

我正在使用Titanium Development中的Google Place API开发应用程序并成功获取数据并在tableview中显示并在每行中添加按钮但是我的问题是我想在每行按钮上获取名称和地址所以请给我一些想法或任何链接来解决我的问题。

请查看我的截图,以便btnclick我想获取姓名和地址。

提前致谢。

var categoryName;   
var addressLabel;
var row;

var tableData=[];

PlacesListCells = function createRow() 
{ 
    //var tableData=[];

    var loader = Titanium.Network.createHTTPClient();

    var url = "https://maps.googleapis.com/maps/api/place/search/json?";    
    url = url + "location=" + lat + ',' + lon;
    url = url + "&radius=" + radius;
    url = url + "&name=" + name;
    url = url + "&sensor=" + sensor;
    url = url + "&key=" + key;

    Ti.API.info(url);
    // Sets the HTTP request method, and the URL to get data from
    loader.open("GET",url);
    // Create our HTTP Client and name it "loader"
    // Runs the function when the data is ready for us to process
    loader.onload = function() 
    {
        var obj = JSON.parse(this.responseText);
        Ti.API.log(obj);    
        var results = obj.results;
        Ti.API.log(results);


        for (var i = 0; i < results.length; i++)
        {
            categoryName = obj.results[i].name; 
            reference = obj.results[i].reference;
            Ti.API.log('Refernce:'+reference);

            // Create a row and set its height to auto
            row = Titanium.UI.createTableViewRow({height:'78'});

            var placeImage =  Titanium.UI.createImageView
            ({
                image:'../iphone/appicon.png',
                width:70,
                height:50,
                top:12,
                left:5
            });

            // Create the label to hold the tweet message
            var nameLabel = Titanium.UI.createLabel({
                //text:name,
                left:80,
                top:5,
                height:'auto',
                width:185,
                textAlign:'left',
                font:{fontSize:12}
            });

            // Create the label to hold the tweet message
            addressLabel = Titanium.UI.createLabel({
                left:80,
                top:25,
                height:'auto',
                width:185,
                textAlign:'left',
                font:{fontSize:14}
            });

            var arrowImage = Ti.UI.createImageView
            ({
                image:'../iphone/appicon.png',
                width:20,
                height:20,
                left:280,
                top:30
            });

            var favoriteButton = Ti.UI.createButton
            ({
                title:'btn',
                //font:{fontFamily:'Helvetica Neue',fontSize:15},
                top:20,
                left:255,
                height:30,
                width:50,
                url:'../Images/favorite.png'
                //image:'../Images/favorite.png'
            });

            nameLabel.text = categoryName;

            getDetailsData(addressLabel,row,i);

            row.add(placeImage);
            row.add(nameLabel);
            row.add(addressLabel);
            row.add(favoriteButton);
            //row.add(arrowImage);

            tableData[i] = row;

            //set page title for each row
            row.pageTitle = nameLabel.text;

            favoriteButton.row = i;
            favoriteButton.addEventListener('click', function(e)
            {
                Ti.API.log('favoriteButton clicked on row ' + e.source.row +' at ' + new Date().getSeconds());
                alert('favoriteButton clicked on row ' + e.source.row);

                var index = e.source.row;
                var name = tableData[index];
                alert('name'+name);

            });
        }       
        tableView.setData(tableData);       
    };
    //-- Network error
    loader.onerror = function(e)
    {
        Ti.API.info('Network error: ' + JSON.stringify(e));
    };

    // Send the HTTP request
    loader.send();

    return  tableData;
};

enter image description here

2 个答案:

答案 0 :(得分:1)

获得索引后,您应该能够引用原始数据源。

答案 1 :(得分:1)

       var favoriteButton = Ti.UI.createButton({
            title:'btn',
            //font:{fontFamily:'Helvetica Neue',fontSize:15},
            top:20,
            left:255,
            height:30,
            width:50,
            url:'../Images/favorite.png'
            //image:'../Images/favorite.png',
            name:categoryName,
            address:address;//address mention here
        });

       row.add(favoriteButton);

        favoriteButton.addEventListener('click', function(e)
         {
            var index = e.source.row;
            var name = e.source.name;
            var address = e.source.address; 
            alert('name'+name);

        });
    } 

我认为这可能对你有很大的帮助。