刷新后不要追加相同的数据 [英] After Refresh dont append the same data

查看:22
本文介绍了刷新后不要追加相同的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次为 Iphone 应用程序使用 appcelerator,我使用的是 TableViewRow,当窗口加载一切正常时,但如果我想刷新窗口,每次我推送刷新时 TableViewRow 都会重复相同的数据,所以,我有 5 个类别,刷新后我每次尝试刷新时都会重复相同的类别.

im using for first time appcelerator for a Iphone app, im using a TableViewRow, when the window load all is fine, but if i want to refresh the window, the TableViewRow repeat the same data every time that i push refresh, so, i have 5 Categories, after refresh im having the same Categories repeting every time that im trying to refresh.

Ti.UI.backgroundColor = '#dddddd';

var url = "http://remoteJSON.js";
var win = Titanium.UI.currentWindow;
var table = Titanium.UI.createTableView();
var tableData = [];
var json, categorias, categoria, i, row, categoriaLabel, descripcionLabel;

var refreshBtn = Titanium.UI.createButton({
    systemButton: Ti.UI.iPhone.SystemButton.REFRESH
});

refreshBtn.addEventListener('click', function() {
    loadTutoriales();
});

win.setRightNavButton(refreshBtn);


var actInd = Titanium.UI.createActivityIndicator({
    bottom:10, 
    height:50,
    width:10,
    style:Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN
});

function loadTutoriales(){

    if(!Titanium.Network.online){
         alert("Debe conectarse a internet.");
    } 

    var xhr = Ti.Network.createHTTPClient({

        onload: function() {
        actInd.show();
        actInd.message = 'Loading...'; 


        //Ti.API.debug(this.responseText);

        json = JSON.parse(this.responseText);
        for (i = 0; i < json.categorias.length; i++) {

                var rowColor = '#eeeeee';
                if(i & 1){
                    rowColor = '#ffffff';
                }


                categoria = json.categorias[i];

                var row = Ti.UI.createTableViewRow({
                    height:'auto',
                    hasChild:true
                });

                row.backgroundColor=rowColor;


                /* add */
                var post_view = Titanium.UI.createView({
                        height:'auto', 
                        layout:'vertical',
                        top:5,
                        right:5,
                 bottom:5,
                 left:5
            });

                var av_image = Titanium.UI.createImageView({
                        image:categoria.imageUrl, 
                        top:0,
                        left:0,
                        height:48,
                        width:48
                });
                post_view.add(av_image);

                var inner_view = Titanium.UI.createView({
                    height:'auto',
                    layout:'vertical', 
                    top:0,          
                    right:0,
                    bottom:0,
                    left:0
                });

               /* end add */

                var categoriaLabel = Ti.UI.createLabel({
                    text:categoria.categoryName,
                    left:54,
                    width:120,
                    top:-48,
                    bottom:0,
                    height:18,
                    textAlign:'left',
                    color:'#444444',
                    font:{fontFamily:'Trebuchet MS',fontSize:14,fontWeight:'bold'}
                });
                inner_view.add(categoriaLabel);

                var descripcionLabel = Ti.UI.createLabel({
                    text:'"' + categoria.description + '"',
                    left:54,
                    top:0,
                    bottom:2,
                    height:'auto', 
                    width:'auto',
                    textAlign:'left',
                    font:{fontSize:14}
                });
                inner_view.add(descripcionLabel);
                post_view.add(inner_view);

                row.add(post_view);
                //row.add(descripcionLabel);
                tableData.push(row);
            }

            table.setData(tableData); 
        },
        onerror: function(e) {
            Ti.API.debug("STATUS: " + this.status);
            Ti.API.debug("TEXT:   " + this.responseText);
            Ti.API.debug("ERROR:  " + e.error);
            alert('There was an error retrieving the remote data. Try again.');
        },
        timeout:5000

    });

        //win.add(actInd);
    win.add(table);
    win.open();
        //actInd.hide();
    xhr.open("GET", url);
    xhr.send();
}
loadTutoriales();

推荐答案

当你按下刷新按钮时,在你再次填充你的表格之前清除 tableData 数组并分配给 table set data 函数.所以它会先清空你的表,然后填充新的刷新数据.

When you press the refresh button clear the tableData array and assign to table set data function before you fill your table again. So it will empty your table first and than fill new refreshed data.

像这样,

refreshBtn.addEventListener('click', function() {
    tableData = [];
    table.setData(tableData);
    loadTutoriales();
});

这篇关于刷新后不要追加相同的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆