Content in View stacking when window comes into focus

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

Hi guys,

I posted a question previously asking how to get a TabBar to refresh when it's brought into focus.

I got that working great.

Problem now is that every time I tap the TabBar, it overlays the content each time.

It's not overly obvious, but I have drop shadows which get darker and darker as you keep taking.

I guess I need to remove the View first perhaps?

Any suggestions? Code below:

var window = Titanium.UI.currentWindow;
 
var setData = function(e){
 
 
var db = Ti.Database.open('DBNAME');
var sql = db.execute('select * from favs');
 
var data = [];
 
while(sql.isValidRow()) {
 
    var name = sql.fieldByName('name');
    var favID = sql.fieldByName('ID');
 
    // Data for each table row
    data.push({
        title:name,
        hasChild:true,
        editable:true,
        color:'#00675a',
        selectedBackgroundColor:'transparent',
        hasDetail: true,
        rightImage: 'images/green_arrow.png',
        leftImage: 'images/fav_share.png',
        url:'favourites_detail.js',
        id:favID
    });
    // Skip onto the next record
    sql.next();
}
 
// close the db
db.close();
 
var tableview = Titanium.UI.createTableView({
    data: data,
    backgroundColor: 'transparent',
    width: 280,
    height: 216,
    top: 97
});
window.add(tableview);
 
tableview.setData(data);
 
 
var HeaderImage = Ti.UI.createImageView({
    image: 'images/home_names.png',
    width: 320,
    bottom:317,
    height:100
});
 
var PatchBGImage = Ti.UI.createImageView({
    image: 'images/favourites_patch_background.png',
    width: 307,
    height:253,
    left:7,
    top:82
});
 
var HeartImage = Ti.UI.createImageView({
    image: 'images/favourites_heart.png',
    width: 58,
    height:53,
    right:16,
    top:37
});
 
// Create a Scroll View to contain the News table
var FavScroller = Titanium.UI.createScrollView({
    backgroundImage: 'images/news_scroll_background.png',
    showVerticalScrollIndicator: true,
    width: 285,
    height: 225,
    contentWidth: 285,
    contentHeight: 225,
    top: 93
});
 
 
// Create the Favourites title in custom font
var FavLabel = Titanium.UI.createLabel({
    text:'Favourites',
    height:'auto',
    width:'auto',
    color:'#00675a',
    font:{fontSize:23, fontFamily:'FS Albert', fontWeight:'bold'},
    textAlign:'left',
    left:18,
    top:39
});
 
// Create the View
var MainView = Titanium.UI.createView({
   width:320,
   height:368
});
 
// Add View to the Window
window.add(MainView);
 
// Add Image Views to the Window
MainView.add(HeaderImage);
MainView.add(PatchBGImage);
MainView.add(HeartImage);
 
// Add the Scroll View to the View
MainView.add(FavScroller);
 
// Add Text to Window
MainView.add(FavLabel);
 
// Add table to the View
MainView.add(tableview);
 
// Add Event listener to table row
tableview.addEventListener('click',function(e)
{
   if(e.rowData.url)
   {
        var detailWindow = Ti.UI.createWindow({
            id:e.rowData.id,
            url:e.rowData.url,
            barColor: '#005349',
            titleImage: 'images/title_gfx.png',
        });
 
        Ti.UI.currentTab.open(detailWindow,{animated:true});
   }
 
 
});
 
}
 
window.addEventListener('focus',function(e){
 
    setData();
});

Cheers,

Simon

1 Answer

Accepted Answer

Move all views, table out of setData. setData should only fetch data from DB then update table.

var window = Titanium.UI.currentWindow;
 
var tableview = Titanium.UI.createTableView({
    // data : data,//---
    backgroundColor : 'transparent',
    width : 280,
    height : 216,
    top : 97
});
window.add(tableview);
 
var HeaderImage = Ti.UI.createImageView({
    image : 'images/home_names.png',
    width : 320,
    bottom : 317,
    height : 100
});
 
var PatchBGImage = Ti.UI.createImageView({
    image : 'images/favourites_patch_background.png',
    width : 307,
    height : 253,
    left : 7,
    top : 82
});
 
var HeartImage = Ti.UI.createImageView({
    image : 'images/favourites_heart.png',
    width : 58,
    height : 53,
    right : 16,
    top : 37
});
 
// Create a Scroll View to contain the News table
var FavScroller = Titanium.UI.createScrollView({
    backgroundImage : 'images/news_scroll_background.png',
    showVerticalScrollIndicator : true,
    width : 285,
    height : 225,
    contentWidth : 285,
    contentHeight : 225,
    top : 93
});
 
// Create the Favourites title in custom font
var FavLabel = Titanium.UI.createLabel({
    text : 'Favourites',
    height : 'auto',
    width : 'auto',
    color : '#00675a',
    font : {
        fontSize : 23,
        fontFamily : 'FS Albert',
        fontWeight : 'bold'
    },
    textAlign : 'left',
    left : 18,
    top : 39
});
 
// Create the View
var MainView = Titanium.UI.createView({
    width : 320,
    height : 368
});
 
// Add View to the Window
window.add(MainView);
 
// Add Image Views to the Window
MainView.add(HeaderImage);
MainView.add(PatchBGImage);
MainView.add(HeartImage);
 
// Add the Scroll View to the View
MainView.add(FavScroller);
 
// Add Text to Window
MainView.add(FavLabel);
 
// Add table to the View
MainView.add(tableview);
 
// Add Event listener to table row
tableview.addEventListener('click', function(e) {
    if(e.rowData.url) {
        var detailWindow = Ti.UI.createWindow({
            id : e.rowData.id,
            url : e.rowData.url,
            barColor : '#005349',
            titleImage : 'images/title_gfx.png',
        });
 
        Ti.UI.currentTab.open(detailWindow, {
            animated : true
        });
    }
 
});
 
var setData = function(e) {
 
    var db = Ti.Database.open('DBNAME');
    var sql = db.execute('select * from favs');
 
    var data = [];
 
    while(sql.isValidRow()) {
 
        var name = sql.fieldByName('name');
        var favID = sql.fieldByName('ID');
 
        // Data for each table row
        data.push({
            title : name,
            hasChild : true,
            editable : true,
            color : '#00675a',
            selectedBackgroundColor : 'transparent',
            hasDetail : true,
            rightImage : 'images/green_arrow.png',
            leftImage : 'images/fav_share.png',
            url : 'favourites_detail.js',
            id : favID
        });
        // Skip onto the next record
        sql.next();
    }
 
    // close the db
    db.close();
 
    tableview.setData([]);
    tableview.setData(data);
}
 
window.addEventListener('focus', function(e) {
    setData();
});

Your Answer

Think you can help? Login to answer this question!