hi there, i am trying to refresh my tableview but it always adding the same values as new record? append to the bottom? i can't understand why? what should i do?
yenile_btn.addEventListener("click",function(e){ var db = Titanium.Database.open('mydb'); var baglanti = db.execute('SELECT id,baslik FROM liste'); Ti.API.info("Toplam Row Say?s?:"+baglanti.getRowCount()); if(!baglanti.getRowCount()){ alert("Hiç Listeniz Yoktur."); } while (baglanti.isValidRow()) { //data.push({id:baglanti.fieldByName('id'), title:baglanti.fieldByName('baslik')}); var renk_ayarla = function(id){ if(id%2==1){ row.backgroundColor = '#1f1f1f'; }else{ row.backgroundColor ='#2a2a2a' ; } } row = Ti.UI.createTableViewRow({ height:30, width:Ti.UI.FILL, selectedBackgroundColor:'#000', hasChild:true, data_id:baglanti.fieldByName('id'), backgroundColor:'' }); renk_ayarla(baglanti.fieldByName('id')) var id = baglanti.fieldByName('id'); var baslik = baglanti.fieldByName('baslik'); nameLabel = Ti.UI.createLabel({ text:baglanti.fieldByName('baslik'), font:{ fontSize:12, fontWeight:'bold', fontFamily:'Trebuchet MS' }, height:'auto', top:0, left:15, height:30, color:'#FFF', shadowColor:'#000', shadowOffset:{ x:1, y:1 } }); row.add(nameLabel); tableData.push(row); baglanti.next(); } table.setData(tableData); table.addEventListener("click",function(e){ alert(e.source.data_id); }); baglanti.close(); db.close(); });
2 Answers
It looks like you are constantly pushing data into the tableData array without ever clearing it. So each time you are just adding more rows to the array. Try setting tableData to an empty array before you start each population or after you call setData() on the table.
Create row,label etc controls with var keyword. Here in above code, the variable which are created, are global one.
while (baglanti.isValidRow()) { //data.push({id:baglanti.fieldByName('id'), title:baglanti.fieldByName('baslik')});
var renk_ayarla = function(id){
var row = Ti.UI.createTableViewRow({
height:30,
width:Ti.UI.FILL,
selectedBackgroundColor:'#000',
hasChild:true,
data_id:baglanti.fieldByName('id'),
backgroundColor:''
});
if(id%2==1){ row.backgroundColor = '#1f1f1f'; }else{ row.backgroundColor ='#2a2a2a' ; }
}
renk_ayarla(baglanti.fieldByName('id'))
var id = baglanti.fieldByName('id');
var baslik = baglanti.fieldByName('baslik');
var nameLabel = Ti.UI.createLabel({
text:baglanti.fieldByName('baslik'),
font:{
fontSize:12,
fontWeight:'bold',
fontFamily:'Trebuchet MS'
},
height:'auto',
top:0,
left:15,
height:30,
color:'#FFF',
shadowColor:'#000',
shadowOffset:{
x:1,
y:1
}
});
row.add(nameLabel);
tableData.push(row);
baglanti.next();
}
Your Answer
Think you can help? Login to answer this question!