Refresh tableview data

You must Login before you can answer or comment on any questions.

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.

— answered 9 months ago by Anthony Decena
answer permalink
3 Comments
  • this is my tableview

    var table = Ti.UI.createTableView({
        top:0,
        width:320,
        left:0,
        backgroundColor:'transparent',
        backgroundImage:'transparent',  
        separatorColor:'transparent',
        background:'transparent',
        data: tableData
    });
    i am adding this line before opening the database
    table.setData([]);
    but still the same problem?

    — commented 9 months ago by Graham Jeffrey

  • Like I said, its because you haven't reset the array tableData. You keep pushing more and more info onto that array. Reset that array:

    tableData = [];
    before you populate each time.

    — commented 9 months ago by Anthony Decena

  • Can you please my code :( i am still trying but couldn't make it :(

    Ti.include("functions.js");
    var baseWin = Ti.UI.currentWindow;
    baseWin.navBarHidden = true;
    baseWin.backgroundColor = '#000';
     
    mywindow = Ti.UI.createWindow({
        title:"Videolar",
        barColor:'#000',
        navBarHidden:false
     
    });
    mywindow.barColor = '#000';
    mywindow.backgroundColor = '#000';
     
    var navGroup = Ti.UI.iPhone.createNavigationGroup( {
        window : mywindow
     
    });
     
     
    var db = Titanium.Database.open('mydb');
    var baglanti = db.execute('SELECT id,baslik FROM liste');
     
    var tableData= [];
     
    var table = Ti.UI.createTableView({
        top:0,
        width:320,
        left:0,
        backgroundColor:'transparent',
        backgroundImage:'transparent',  
        separatorColor:'transparent',
        background:'transparent'
    });
     
    mywindow.add(table);
     
    var ekle = Ti.UI.createButton({
        title:'Yeni'
    });
     
    var refresh = Ti.UI.createButton({
        title:'Refresh'
    });
     
    mywindow.setLeftNavButton(refresh);
    mywindow.setRightNavButton(ekle);
    ekle.addEventListener("click",function(e){
        var ekle_pencere = Ti.UI.createWindow({
            title:'Yeni Liste Ekle',
            barColor:'#000'
        });
        var isim = Ti.UI.createTextField({
            color:'#000',
                                    top:10,
                                    left:10,
                                    right:10,
                                    width:300,
                                    height:100,
                                    hintText:'Liste Ad?',
                                    keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
                                    returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
                                    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
        });
     
        var gonder = Ti.UI.createButton({
            title:"Ekle",
            width:50,
            height:30,
            backgroundColor:'#1e1e1e',
            top:150,
            right:50        
        });
     
        ekle_pencere.addEventListener("click",function(e){
            isim.blur();
        });
     
        gonder.addEventListener("click",function(e){
            liste_ekle(isim.value);
     
            tableData.push(row);
            navGroup.close(ekle_pencere);
            navGroup.open(mywindow);
        });
     
        ekle_pencere.add(isim);
        ekle_pencere.add(gonder);
        navGroup.open(ekle_pencere);
    });
     
     
     
    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);
     
        });
     
        refresh.addEventListener("click",function(e){
        tableData = [];
    });
     
    /* here's the tableview part */
     
    mywindow.add(table);
    mywindow.navGroup = navGroup;
    baseWin.add(navGroup);
    baglanti.close();
    db.close();

    — commented 9 months ago by Graham Jeffrey

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!