Json负载期间的Appcelerator Titainium活动指示器

时间:2012-03-19 03:56:20

标签: appcelerator titanium-mobile

如何在加载json时给出活动指示器?这里是我的代码,

我想在阅读json的应用程序中给出活动指示符:)

数据将加载20段长度,(来自json),当我的应用程序尝试读取它时,我想提供活动指示器,使用户不在空白屏幕中等待:)

// Create variable "win" to refer to current window
var win = Titanium.UI.currentWindow;
///window startup
///////////

function loadTweets() {
    // Empty array "rowData" for our tableview
    var rowData = [];

    // Create our HTTP Client and name it "loader"
    var loader = Ti.Network.createHTTPClient({
        onerror : function(e) {
            alert('Koneksi Time Out');
        },
        timeout : 30000
    });
    // Sets the HTTP request method, and the URL to get data from
    loader.open("GET", "http://www.sportku.com/api/get_latest_news/" + win.id);
    // Runs the function when the data is ready for us to process
    loader.onload = function() {
        var tweets = eval('(' + this.responseText + ')');
        for(var i = 0; i < tweets.count; i++) {
            var tweet = tweets.posts[i].excerpt;
            // The tweet message
            var user = tweets.posts[i].title;
            // The screen name of the user
            var avatar = tweets.posts[i].thubmnail;
            // The profile image
            var url = tweets.posts[i].url;
            // Create a row and set its height to auto
            var row = Titanium.UI.createTableViewRow({
                height : 'auto'
            });

            // Create the view that will contain the text and avatar
            var post_view = Titanium.UI.createView({
                height : 'auto',
                layout : 'vertical',
                top : 5,
                right : 5,
                bottom : 5,
                left : 5
            });
            // Create image view to hold profile pic
            var av_image = Titanium.UI.createImageView({
                image : avatar, // the image for the image view
                top : 0,
                left : 0,
                height : 58,
                width : 58
            });
            post_view.add(av_image);
            // Create the label to hold the screen name
            var user_lbl = Titanium.UI.createLabel({
                text : user,
                left : 64,
                width : 226,
                top : -58,
                bottom : 2,
                height : 44,
                textAlign : 'left',
                color : 'black',
                font : {
                    fontFamily : 'Arial',
                    fontWeight : 'bold',
                    fontSize : 12
                }
            });
            post_view.add(user_lbl);
            // Create the label to hold the tweet message
            var tweet_lbl = Titanium.UI.createLabel({
                text : tweet,
                left : 64,
                top : 0,
                bottom : 2,
                height : 'auto',
                width : 226,
                textAlign : 'left',
                font : {
                    fontSize : 11
                }
            });
            post_view.add(tweet_lbl);
            var urls = Titanium.UI.createLabel({
                text : url,
            });
            post_view.add(urls);
            // Add the post view to the row
            row.add(post_view);
            row.hasChild = true;
            //row.hasChild=true;
            // Give each row a class name
            row.className = "item" + i;
            // Add row to the rowData array
            rowData[i] = row;

            post_view.addEventListener('click', function(e) {
                var url_output = e.row.children[0].children[3].text;
                //alert(url_output);
                var webview = Titanium.UI.createWebView({
                    url : url_output
                });
                var window = Titanium.UI.createWindow({
                    backgroundColor : "#fff",
                    barImage : 'image/navbar.png',
                    titleImage : 'image/logo.png',

                });
                window.add(webview);
                window.open({
                    modal : true,
                    modalTransitionStyle : Ti.UI.iPhone.MODAL_TRANSITION_STYLE_FLIP_HORIZONTAL,
                    modalStyle : Ti.UI.iPhone.MODAL_TRANSITION_STYLE_FLIP_HORIZONTAL,
                });
                var buttonImage = [{
                    image : 'image/share.png',
                    width : 30,
                    height : 30
                }]
                var back = Ti.UI.createButtonBar({
                    labels : buttonImage,
                    backgroundColor : 'black',
                    style : Titanium.UI.iPhone.SystemButtonStyle.BAR
                });

                back.addEventListener('click', function() {
                    window.close();
                });

                window.leftNavButton = back;

            });
        }
        // Create the table view and set its data source to "rowData" array
        var tableView = Titanium.UI.createTableView({
            data : rowData
        });
        //Add the table view to the window

        win.add(tableView);
    };
    // Send the HTTP request
    loader.send();
}

loadTweets();

我试着给我的代码,但总是错误:),,

1 个答案:

答案 0 :(得分:3)

您应该做的是先创建活动指标。不需要将活动指示符添加到窗口中,只需要调用show()方法,因此在执行xhr.send()方法之前执行此操作。比onLoad函数的最后一行应该执行indicator.hide()方法。