Data passed from SQLite to webview doesn't load on first click, but does there after.

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

I have data being passed from a SQLite database to a webview. The webview safe itself loads with the HTML and CSS, but the data doesn't. But, if I go back to the table and click the row it will load, and works fine after that. If I close the app down, and go out of the app, the process will repeat. How do I get the data to load into the webview on the first click?

— asked 9 months ago by Michael Zaladonis
3 Comments
  • This is extremely hard to give an answer to because of the lack of code. I know it makes sense when you are trying to explain it, but it doesn't quite make that much sense to those of us that are listening. Please try to provide a minimal code sample in this case that duplicates your problem. Along with your code sample, please provide the information necessary for devs to duplicate your environment. The QA Checklist will help you provide the necessary info.

    — commented 9 months ago by Anthony Decena

  • var win = Ti.UI.currentWindow;
    Ti.UI.currentWindow.barImage = '../images/menubar.png';
    Ti.UI.currentWindow.barColor = '#57a8e2';
    Ti.include('../search/search.js');
     
    var searchBar = Titanium.UI.createSearchBar({
        backgroundImage : '../images/searchbar-bg.png',
        borderStyle : Titanium.UI.INPUT_BORDERSTYLE_NONE,
        showCancel : true,
        height : 44,
        top : 0
    });
    win.add(searchBar);
     
    var tableView = Ti.UI.createTableView({
        top : 44,
        height : 'auto',
        backgroundColor : 'transparent',
            separatorStyle : Titanium.UI.iPhone.TableViewSeparatorStyle.NONE,
     
    });
     
    win.add(tableView);
     
    var row = Titanium.UI.createTableViewRow({
        height : 44,
        backgroundImage : '../images/list-item-bg.png',
     
    });
    var view = Titanium.UI.createView({
        backgroundImage : '../images/list-item-bg.png',
     
    });
     
    row.height = 'auto';
    row.add(view);
     
    // set the data from the database to the array
    function setData() {
     
        var db = Ti.Database.install('../products.sqlite', 'products');
     
        var rows = db.execute('SELECT DISTINCT short FROM labVals');
     
        // create the array
        var dataArray = [];
     
        while (rows.isValidRow()) {
            dataArray.push({
                title : '' + rows.fieldByName('short') + '',
                hasChild : true,
                backgroundImage : '../images/list-item-bg.png',
                path : '../labsind/products.js'
            });
            rows.next();
        };
     
        // set the array to the tableView
        tableView.setData(dataArray);
    };
     
    // create table view
     
    // add the tableView to the current window
    win.add(tableView);
     
    // call the setData function to attach the database results to the array
    setData();
     
    searchBar.addEventListener('cancel', function(e) {
        searchBar.blur();
    });
     
    searchBar.addEventListener('return', function(e) {
        dataArray = search(e.value);
        tableView.setData(dataArray);
        searchBar.blur();
    });
     
    tableView.addEventListener('click', function(e) {
        if (e.rowData.title) {
            var win = Ti.UI.createWindow({
                title : e.rowData.title,
                separatorStyle : Titanium.UI.iPhone.TableViewSeparatorStyle.NONE,
                barImage : '../images/menubar.png',
                backgroundImage : 'images/bg1.png',
                barColor: '#57a8e2',
            });
     
            var productID = e.rowData.title;
     
            var btn = Ti.UI.createButton({
                title : 'Save',
                style : Titanium.UI.iPhone.SystemButtonStyle.BORDERED,
                width : 100
            });
            win.rightNavButton = btn;
     
            var web = Ti.UI.createWebView({
                url : '../sind/productsWebview.html'
            });
     
            var actWin = Ti.UI.createWindow({
                backgroundColor : '#000',
                opacity : 0.8
            });
     
            var actInd = Ti.UI.createActivityIndicator({
                style : Ti.UI.iPhone.ActivityIndicatorStyle.PLAIN
            });
     
            actWin.add(actInd);
     
            if (Ti.Platform.name == 'iPhone OS') {
                actInd.show();
                actInd.color = '#FFF';
                actInd.message = 'Loading...';
                setTimeout(function() {
                    actInd.hide();
                }, 1500);
            };
            setTimeout(function() {
                actWin.hide();
            }, 1500);
     
            var db2 = Ti.Database.install('../products.sqlite', 'products3');
            var specs = db2.execute('SELECT * FROM lVals WHERE short="' + e.rowData.title + '"');
            var productID = specs.fieldByName('id');
            var name = specs.fieldByName('short');
            var pwidth = specs.fieldByName('long');
            var pheight = specs.fieldByName('metric');
            var pcolor = specs.fieldByName('imperial');
            var qty = specs.fieldByName('critical');
            var pcolor1 = specs.fieldByName('web1');
            var qty1 = specs.fieldByName('web2');
            var pcolor2 = specs.fieldByName('web3');
     
            // Add the webView to the window
            win.add(web, actWin);
     
            // Create a timeout - we want time for the window to be ready before we fire the event
            setTimeout(function() {
                Ti.App.fireEvent("webPageReady", {
                    name : name,
                    pwidth : pwidth,
                    pheight : pheight,
                    pcolor : pcolor,
                    qty : qty,
                    pcolor1 : pcolor1,
                    qty1 : qty1,
                    pcolor2 : pcolor2,
     
                    productID : productID,
                });
            }, 500);
     
            btn.addEventListener('click', function(e) {
                productID = productID;
                var favorite = 1;
                // We'll open an instance of the DB here for writing
                var faveDB = Ti.Database.install('../products.sqlite', 'products1');
                faveDB.execute('UPDATE labVals SET favs = ? WHERE id = ?', favorite, productID);
     
                faveDB.close();
                alert("Favs Updated");
                // Fire a custom event telling the application that you've updated the favorites
                Ti.App.fireEvent("favoritesUpdated");
            });
            Ti.UI.currentTab.open(win, {
                animated : true
            });
        };
    });
    search();

    — commented 9 months ago by Michael Zaladonis

  • I am using Titanium 2.1.2 with iOS 5.1 in the emulator and iOS6 on my device.

    — commented 9 months ago by Michael Zaladonis

1 Answer

Try increasing the timeout before you send your Ti.App.fireEvent(webPageReady... event.

Set this to something massive, say 5000 ms and if your webview always updates, it would seem to imply you're firing the event before your webview has loaded.

If that works, refactor your code, firing the event from within a "loaded" event listener attached to your webview.

i.e. something like

web.addEventListener('load', function (e) {
            Ti.App.fireEvent("webPageReady", {
                name : name,
                pwidth : pwidth,
                pheight : pheight,
                pcolor : pcolor,
                qty : qty,
                pcolor1 : pcolor1,
                qty1 : qty1,
                pcolor2 : pcolor2,
                productID : productID,
            });
}

Your Answer

Think you can help? Login to answer this question!