Search code examples
androidappceleratorappcelerator-mobile

tableView filled with database information


I'm building a calander app for Android in appcelerator, but now I'm having some problems getting the information from a database into a tableView. I'm new at appcelerator and have some questions.

I created some tables in sql that I'm getting with a php script. But now that I'm trying to get that information from my database in my app, something goes wrong. Is it possible do to so using a php script or do I have to use something else? (If you visit the link you can see the php script that creates xml. I'll probably change it so the event will show per date: 2012/1/2 GFT, Restafval, PMD; 2012/1/17 papier en karton; etc)

var win = Titanium.UI.currentWindow;

var data = [];

var xhr = Ti.Network.createHTTPClient();
xhr.open('GET', 'http://www.dehertoghnathalie2011.dreamhosters.com/titanium/kalender.php');

xhr.onload = function(){
    try
    {
        var doc = this.responseXML.documentElement;
        var items = doc.getElementsByTagName('gebeurtenis');
        var x = 0;

        for(var i = 0; i<items.length; i++)
        {
            var afhaaldag = items.gebeurtenis(i);
            var datum = gebeurtenis.getElementsByTagName('datum').gebeurtenis(0).text;
            var afval = gebeurtenis.getElementsByTagName('afval').gebeurtenis(0).text;

            var row = Ti.UI.createTableViewRow({height:80});
            var labelText = datum;

            var label = Ti.UI.createLabel({
                text:labelText,
                color:'black',
                textAlign:'left'
            });
            row.add(label);

            data[x++] = row;
        }
    }

    catch(E)
    {
        alert(E);
    }

    var table = Ti.UI.createTableView({data:data});
    win.add(table);
}

xhr.send();

So When I run this in an Android emulator I'm getting an exception: "Cannot find function gebeurtenis in object [Ti.NodeList]." Does anyone know how I can resolve this?

And also: what's the best way for handeling information from and to a database? Is it best to use httpClient (xhr) or SQLite? If I understand it, SQLite is used for storage on the device (like preferences or in my case when someone wants to be notified of an upcoming event) and httpClient is used for RSS. Can you give me other examples for httpClient?

Thanks!


Solution

  • again solved it. I changed these lines:

    var afhaaldag = items.gebeurtenis(i);
    var datum = gebeurtenis.getElementsByTagName('datum').gebeurtenis(0).text;
    var afval = gebeurtenis.getElementsByTagName('afval').gebeurtenis(0).text;
    

    to

    var item = items.item(i);
    var datum = item.getElementsByTagName('datum').item(0).text;
    var afval = item.getElementsByTagName('afval').item(0).text;