Quantcast
Channel: Appcelerator Developer Center Q&A Tag Feed (android)
Viewing all articles
Browse latest Browse all 7655

Display Json data fetched by webservice call in a TableView format using Titanium Alloy

$
0
0

Hi,

I am building a mobile application both for iPhone and Android using the latest version of Titanium Alloy and Titanium SDK : 3.1.2

I need to display JSON data by calling a web-service in a table view format.

I have been able to display the data using the conventional method used for Traditional Titanium as below:

// Please note I am hiding my actual URL for security reasons.
var url = "http://xxxxxxxxxxx_myurl";
 
var win = Ti.UI.createWindow();
var table = Ti.UI.createTableView();
var tableData = [];
var json, deals, deal, i, row, nameLabel, nickLabel;
 
var xhr = Ti.Network.createHTTPClient({
    onload: function() {
    // Ti.API.debug(this.responseText);
 
    json = JSON.parse(this.responseText);
    for (i = 0; i < json.deals.length; i++) {
        deal = json.deals[i];
        row = Ti.UI.createTableViewRow({
            height:'60dp'
        });
 
        // Deal Id row
        idLabel = Ti.UI.createLabel({
            text:deal.dealid,
            font:{
                fontSize:'16dp',
            fontWeight:'bold'
        },
        height:'auto',
        left:'10dp',
        top:'5dp',
        color:'#000',
        touchEnabled:false
        });
 
 
        //Deal Vendor Row
        vendorLabel = Ti.UI.createLabel({
        text:'"' + deal.vendor + '"',
        font:{
            fontSize:'16dp'
        },
        height:'auto',
        left:'15dp',
        top:'25dp',
        bottom:'5dp',
        color:'#000',
        touchEnabled:false
        });
 
 
        row.add(idLabel);
        row.add(vendorLabel);
        tableData.push(row);
        }
 
    table.setData(tableData);
    },
    onerror: function(e) {
    Ti.API.debug("STATUS: " + this.status);
    Ti.API.debug("TEXT:   " + this.responseText);
    Ti.API.debug("ERROR:  " + e.error);
    alert('There was an error retrieving the remote data. Try again.');
    },
    timeout:5000
});
 
xhr.open("GET", url);
xhr.send();
 
win.add(table);
win.open();
But I have found that we need to use Model, Collections and Adapters to display the data in Titanium Alloy. Everything is quite new for me so I would appreciate if anybody could give me a working example where I fetch JSON data from a web-service and use Models/ Collections to display it.

Or, should I stick to the traditional way of displaying the data as above as I understand it?

Please advise. Many thanks


Viewing all articles
Browse latest Browse all 7655

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>