cookbook chapter 2/recipe 3

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

hey to all I've been reading the appcelerator development cookbook...im very curious on chapter 2/ recipe 3..i have a tableview..which is suppose to put remote data xml....but I'm confused about the data and xml....and i want to know why the tableview is empty..with no recipes showing???

var win = Ti.UI.currentWindow;
 
//declare the http client object
var xhr = Titanium.Network.createHTTPClient();  
 
var data = []; //empty data array
var tblRecipes = Titanium.UI.createTableView({
    height: 366,
    width: 320,
    top: 0,
    left: 0,
    rowHeight: 70
});
win.add(tblRecipes);
 
//this method will process the remote data
xhr.onload = function() { 
   var xml = this.responseXML;
 
   //get the item nodelist from our response xml object 
   var items = xml.documentElement.getElementsByTagName("item");
 
   //loop each item in the xml
   for (var i = 0; i < items.length; i++) {
 
        //create a table row
        var row = Titanium.UI.createTableViewRow({
            hasChild: true,
            className: 'recipe-row'
        });
 
        //title label
        var titleLabel = Titanium.UI.createLabel({
            text: items.item(i).getElementsByTagName("title").item(0).text,
            font: {fontSize: 14, fontWeight: 'bold'},
            left: 70,
            top: 5,
            height: 20,
            width: 210
        });
        row.add(titleLabel);
 
        //description label
        var descriptionLabel = Titanium.UI.createLabel({
            text: items.item(i).getElementsByTagName("description").item(0).text,
            font: {fontSize: 10, fontWeight: 'normal'},
            left: 70,
            top: 25,
            height: 40,
            width: 200
        });
        if(descriptionLabel.text == '') {
            descriptionLabel.text = 'No description is available.';
        }
        row.add(descriptionLabel);
 
        //add our little icon to the left of the row
        var iconImage = Titanium.UI.createImageView({
            image: 'images/foodicon.jpg',
            width: 50,
            height: 50,
            left: 10,
            top: 10
        });
        row.add(iconImage);
 
        //add the table row to our data[] object
        data.push(row);
   }
 
   //finally, set the data property of the tableView to our data[] object
   tblRecipes.data = data;   
};

— asked 11 months ago by Ronny Rodriguez
1 Comment
  • can you post the complete sample code? probably in pastebin or gist?

    — commented 11 months ago by Aaron Saunders

1 Answer

You don't show calls to xhr.open() and xhr.send(). Maybe you didn't grab those lines when you copied and pasted.

But if you don't have those calls, you're not making a network request, and the onload() function won't get called.

— answered 11 months ago by Jason Priebe
answer permalink
1 Comment
  • Just a follow on to this question....I am stuck on capter 2/recipe 2.......which is the same issue. This is the code Im using:


    var win = Titanium.UI.currentWindow;

    //declare the http client object

    var xhr = Titanium.Network.createHTTPClient();

    var data = []; //empty data array var tblRecipes = Titanium.UI.createTableView({ height: 366, width: 320, top: 0, left: 0 }); win.add(tblRecipes);

    //this method will process the remote data

    xhr.onload = function() { var xml = this.responseXML;

    //get the item nodelist from our response xml object

    var items = xml.documentElement.getElementsByTagName("item");

    //loop each item in the xml

    for (var i = 0; i < items.length; i++) {

        //create a table row
    
        var row = Titanium.UI.createTableViewRow({
            title: items.item(i).getElementsByTagName(&quot;title&quot;).item(0).text
        });
    
        //add the table row to our data[] object
        data.push(row);
    

    }

    //finally, set the data property of the tableView to our data[] object

    tblRecipes.data = data;

    };

    //this method will fire if there's an error in accessing the remote data

    xhr.onerror = function() {
    //log the error to our titanium developer console Ti.API.error(this.status + ' - ' + this.statusText); };

    //open up the recipes xml feed

    xhr.open('GET', 'http://www.cuisine.com.au/feed/all-recipes');

    //finally, execute the call to the remote feed

    xhr.send();


    I am invoking xhr.open and xhr.send but I still get an empty TableView and no data. At the first line of the onload function(this.responseXML) the console shows this error: [ERROR] Error Domain=com.google.GDataXML Code=-1 "The operation couldn’t be completed. (com.google.GDataXML error -1.)". in -[TiDOMDocumentProxy parseString:] (TiDOMDocumentProxy.m:57)

    Any help will be welcome.

    — commented 10 months ago by Adarsh Hasija

Your Answer

Think you can help? Login to answer this question!