显示自定义tableview行

时间:2012-10-17 15:40:52

标签: database titanium-mobile

我正在尝试从本地数据库中提取数据并将其显示在使用自定义行的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);
}
}

1 个答案:

答案 0 :(得分:2)

你是对的,2虽然循环有问题。 您还需要向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);
}
相关问题