2012-10-17 94 views
0

我試圖從本地數據庫中提取數據,並將其顯示在使用自定義行的tableview中。我不知道如何讓它正確顯示。它當前寫入的方式將顯示一行,其中的數據正確顯示,但它似乎顯示了成千上萬的空行。我認爲我的問題是因爲我有兩個while (rows.isValidRow())陳述。請有人向我展示使該顯示正確的代碼?這裏是我目前的代碼:顯示自定義表格行

//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'; 

    dataArray.push(row); 
    rows.next();  
} 

// set the array to the tableView 
tableview.setData(dataArray); 
} 
} 
+0

問題解決!上面的代碼已被編輯以反映可用的代碼! – dnevels

回答

2

你是對的,2 while while循環出問題。 還需要將行添加到dataArray中:dataArray.push(row);

然後while循環之外,設定dataArray中的數據的tableView:tableview.setData(dataArray);

我只是修改了一些你的代碼,它是工作的罰款:

//Setup the window 
var win = Titanium.UI.createWindow({ 
    backgroundColor: '#123456', 
}); 

//install database 
var db = Ti.Database.open('distributor'); 
try{ 
    db.execute('create table if not exists distributor(id varchar(10), distributor_name varchar(10), distributor_address varchar(10), distance varchar(10));'); 
    db.execute("Insert into distributor values('id1','name1','address1','distance1')"); 
    db.execute("Insert into distributor values('id2','name2','address2','distance2')"); 
    db.execute("Insert into distributor values('id3','name3','address3','distance3')"); 
    db.execute("Insert into distributor values('id4','name4','address4','distance4')"); 
    db.execute("Insert into distributor values('id5','name5','address5','distance5')"); 
} 
catch(err) 
{ 
    Ti.API.info(err.message); 

} 



// create table view 
var tableview = Ti.UI.createTableView({ 
    backgroundColor:'transparent', 
    color: '#000000', 
    top:80, 
    height:'auto', 
    left:80, 
    right:80, 
    bottom:120 
}); 

setData(); 
win.add(tableview); 
win.open(); 


function setData() { 
    //db.open(); 
    var rows = db.execute('SELECT * FROM distributor'); 
    //db.close(); 
    var dataArray = []; 
    while (rows.isValidRow()) 
    { 
     var row = Ti.UI.createTableViewRow({ 
      haschild: true, 
     }); 
     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); 
     dataArray.push(row); 
     rows.next();  
    } 
    tableview.setData(dataArray); 
}