Titanium Mobile SDK version: 2.1.2 (08/24/12 14:46 ed7f777), iPhone SDK version: 5.1, Titanium Studio, build: 3.0.1.201210141904, OSX 10.7.5.
I am attempting to pull data from the local database and display it in a tableview that uses a custom row. I do not know how to get it to display properly. the way it is currently written will display one row with the data correctly displayed in it, but it also appears to be showing thousands of empty rows. My database right now only has 6 entries. I think my problem is because I have two while (rows.isValidRow()) statements. Would someone please show me the code to make this display correctly? Here is my current code:
//Setup the window var win = Titanium.UI.currentWindow; win.barColor='#000000'; //install database var db = Ti.Database.install('bcapool3.sqlite','distributor'); //Get data var rows = db.execute('SELECT * FROM distributor'); // create table view var tableview = Ti.UI.createTableView({ backgroundColor:'transparent', color: '#000000', top:80, height:'auto', left:80, right:80, bottom:120 }); function setData() { while (rows.isValidRow()) { var dataArray = []; var row = Ti.UI.createTableViewRow({ haschild: true, path: 'distributordetail.js' }); var title = Ti.UI.createLabel({ color:'#000000', height:32, left:5, top:2, font:{fontSize:'18dp',fontWeight:'bold' }, backgroundColor:'transparent', text:'' + rows.fieldByName('distributor_name') + '' }); var address = Ti.UI.createLabel({ color:'#000000', height:32, left:5, top:34, fontSize:'14dp', backgroundColor:'transparent', text:'' + rows.fieldByName('distributor_address') + '' }); var distance = Ti.UI.createLabel({ color:'#000000', height:32, right: 10, top:34, fontSize:'12dp', backgroundColor:'transparent', text:'' + rows.fieldByName('distance') + '' }); row.add(title); row.add(address); row.add(distance); tableview.add(row); row.className = 'distributorRow'; while (rows.isValidRow()) { dataArray.push(row); rows.next(); } // set the array to the tableView tableview.setData(dataArray); } }
1 Answer
Accepted Answer
function setData() { var dataArray = []; while (rows.isValidRow()) { var row = Ti.UI.createTableViewRow({ haschild: true, path: 'distributordetail.js' }); var title = Ti.UI.createLabel({ color:'#000000', height:32, left:5, top:2, font:{fontSize:'18dp',fontWeight:'bold' }, backgroundColor:'transparent', text:'' + rows.fieldByName('distributor_name') + '' }); var address = Ti.UI.createLabel({ color:'#000000', height:32, left:5, top:34, fontSize:'14dp', backgroundColor:'transparent', text:'' + rows.fieldByName('distributor_address') + '' }); var distance = Ti.UI.createLabel({ color:'#000000', height:32, right: 10, top:34, fontSize:'12dp', backgroundColor:'transparent', text:'' + rows.fieldByName('distance') + '' }); row.add(title); row.add(address); row.add(distance); tableview.add(row); row.className = 'distributorRow'; dataArray.push(row); rows.next(); } // set the array to the tableView tableview.setData(dataArray); }
Your Answer
Think you can help? Login to answer this question!